My website is made with Ghost CMS. It’s node.js based. So I guess i must use the Javascript options.
I tried including the snippet code provided in NocoDB. But nothing shows on my website. Looked into the swagger thing. But code sample are only in Curl.
Do I need to put the xcToken too? Where?
Do I need to add more code to call data? How?
Is there a full tutorial on how to handle api request in javascript with nocodb?
Your fetch call is looks good, it should print out the response to developer console (F12 or Inspect in right-click menu and look for Console tab).
An easier way for a beginner would be using embed, you can share any table you want (for reference Share view | NocoDB) and click button near Copy URL to get iframe code which you can directly embed to your page.
If you want to directly fetch and use APIs (as you’re doing now) you can check this or similar tutorials.
Here’s what I have so far. I have a Unauthorized access message from the console. But I dont know where to implement the xc-auth token in the code.
// api url
const api_url =
"https://nocodb.mydomain.com/api/v1/db/data/v1/Project/MyTablelimit=25&shuffle=0&offset=0";
// Defining async function
async function getapi(url) {
// Storing response
const response = await fetch(url);
// Storing data in form of JSON
var data = await response.json();
console.log(data);
if (response) {
hideloader();
}
show(data);
}
// Calling that async function
getapi(api_url);
// Function to hide the loader
function hideloader() {
document.getElementById('loading').style.display = 'none';
}
// Function to define innerHTML for HTML table
function show(data) {
let tab =
`<tr>
<th>Name</th>
<th>Office</th>
<th>Position</th>
<th>Salary</th>
</tr>`;
// Loop to access all rows
for (let r of data.list) {
tab += `<tr>
<td>${r.name} </td>
<td>${r.office}</td>
<td>${r.position}</td>
<td>${r.salary}</td>
</tr>`;
}
// Setting innerHTML as tab variable
document.getElementById("employees").innerHTML = tab;
}