I have a server that consistently updates a JSON file. Tho the code I setup bellow (javascript) reads the JSON file and shows it to the client always refreshes the page.
I would like to know how I would be able to read my JSON file every time it's updated without refreshing the web page.
From what I looked up, it seems that I'll have to use AJAX to get this done. But couldn't find much else. Or should I make any update on my JSON file?
This is the index.html code I'm using to get the data from my archive.json file:
<script>
fetch('archive.json')
.then(function (response) {
return response.json();
})
.then(function (data) {
appendData(data);
})
.catch(function (err) {
console.log('error: ' + err);
});
function appendData(data) {
console.log(data.velas.length);
var mainContainer = document.getElementById("myData");
for (var i = 0; i < data.velas.length; i++) {
var div = document.createElement("div");
div.innerHTML = 'Tempo: ' + data.velas[i].tempo;
mainContainer.appendChild(div);
}
}
</script>
Thanks for any help!