I have a server that automatically updates a JSON file. However, the JavaScript code I have implemented below reads the JSON file and displays it to the client, but it always refreshes the page.
I am looking for a solution on how to read my JSON file every time it gets updated without having to refresh the webpage.
After searching online, it appears that I may need to use AJAX to achieve this. However, I couldn't find much information beyond that. Do I need to make any updates to my JSON file itself?
Below is the snippet of index.html code that I am currently using to fetch data from archive.json:
<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>
Any assistance or guidance would be greatly appreciated!