0

I'm running this simple javascript on a Flask server, the idea is that whenever there are changes on the status.json file they are shown in the text_here div without needing to reload.

function refreshdata(){
    $.getJSON("static/json/status.json", function(data){
        document.getElementById("text_here").innerHTML = JSON.stringify(data, null, 2)
        console.log("success")
    });
}

refreshdata();
setInterval(function(){
    refreshdata()
},5000)

The html includes the line <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>.

When I first load the page the json file is rendered properly, but when I make changes on the json they don't reflect on the page afterwards. In fact, it doesn't even show any changes unless I hard reload the page. The script does console log 'sucess' every time though. Any way to fix it? thanks

  • it is possible that the browser has "cached" result of ```static/json/status.json``` – Varinder Feb 21 '18 at 01:03
  • I think that's very possible, but I don't know how to fix it if that's the case – Grman Rodriguez Feb 21 '18 at 01:03
  • debugging 101: try logging `data` instead. If the JSON file is cached, you need to set up no-cache headers in flask when sending it. –  Feb 21 '18 at 01:04
  • Nope, still logs the data it had when first loaded – Grman Rodriguez Feb 21 '18 at 01:05
  • Either the json is not changing, the requests aren't firing properly, or the result is getting cached. Check the network tab in Chrome developer console and see if the requests are coming in properly – gtalarico Feb 21 '18 at 01:11
  • The file is changing because the changes reflect when I hard reload the page, and the network tab shows the xhr request being made for status.json every 5 seconds. So it has to be cache. – Grman Rodriguez Feb 21 '18 at 01:16
  • @ChrisG I implemented the `nocache` wrapper from the link you provided in my Flask code but it's still caching and I don't know how to proceed – Grman Rodriguez Feb 21 '18 at 01:17
  • Wait, I used another code from the same link @ChrisG provided and it was fixed. Thanks everybody! – Grman Rodriguez Feb 21 '18 at 01:25
  • @GrmanRodriguez please consider adding your solution by answering your own post so others can benefit from it in the future. https://stackoverflow.com/help/self-answer – gtalarico Feb 21 '18 at 05:13
  • Post is already closed, but anyway all I did was implement @soff_df code in my Flask server from this post: https://stackoverflow.com/questions/34066804/disabling-caching-in-flask – Grman Rodriguez Feb 21 '18 at 13:32

0 Answers0