I have
/mysite/index.html
Then I have a folder /mysite/products/ with JSON files as following:
/mysite/products/1.json
/mysite/products/2.json
/mysite/products/3.json
…
/mysite/products/578.json
and etc…
Every JSON file looks like this:
[{"id":1,"name":"16' Tire Japan","stock":145}]
In my HTML I have a table:
<table>
<thead>
<th>id</th>
<th>name</th>
<th>stock</th>
</thead>
<tbody id="products"></tbody>
</table>
Then I have a file /mysite/script.html connected with my HTML.
How can I fetch all existing .JSON files one by one from my /products folder into the table tbody (id=products) using JavaScript?
Now I have one JSON file for all products and I use fetch() like this:
fetch("products.json")
.then(function(response){
return response.json();
})
.then(function(products){
let placeholder = document.querySelector("#products");
let out = "";
for(let product of products){
out += `<tr id='tr${product.id}'>
<td class='id'>${product.id}</td>
<td class='name'>${product.name}</td>
<td class='stock'>${product.stock}</td>
</tr>`;
}
placeholder.innerHTML = out;
});
New contributor
Ирина Комсомольская is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
7