Im trying to display my json-data in an HTML page, but it wont work.
<div id="varer"></div>
<div id="myData"></div>
<script>
fetch('c:/Users/name/school/class/marketplace/data/products.json')
.then(function (response) {
return response.json();
})
.then(function (data) {
appendData(data);
})
.catch(function (err) {
console.log('error: ' + err);
});
function appendData(data) {
var mainContainer = document.getElementById("myData");
for (var i = 0; i < data.length; i++) {
var div = document.createElement("div");
div.innerHTML = 'Name: ' + data[i].firstName + ' ' + data[i].lastName;
mainContainer.appendChild(div);
}
}
</script>
My JSON file is in another directory than the HTML page. When i run this in the browser console, its says that my API fetch cannot load.