0
$.ajax({
     url: "https://mynas.com/myfile.json",
     cache: false,
     dataType: "text",
     success: function (data) {

     //calling this on a 5 second interval
     //data does not reflect updates unless the page is refreshed

     },
     error: function (data) {
         console.log("Bummer...");
     }
});

If I make the call above at 5 second intervals I always get the data returned by the first call even if I update and save myfile.json on my server. Shouldn't setting cache false cause a fresh data pull? Are the rules different for text files as opposed to Web API calls?

If I refresh the page in the browser new data is pulled.

The file is stored on a NAS drive attached to an IIS server.

I have also tried:

$.ajaxSetup({ cache: false });
Abhishek Bhagate
  • 5,033
  • 3
  • 10
  • 29
Robert
  • 820
  • 2
  • 12
  • 26
  • 5 Seconds!? It's possible the JSON has not even loaded fully by the time you make the next request. You could also try to alter the URL: `"https://mynas.com/myfile.json?" + Date.now()` – Twisty Jun 25 '20 at 20:44
  • Possible Duplicate: https://stackoverflow.com/questions/367786/prevent-browser-caching-of-ajax-call-result – Twisty Jun 25 '20 at 20:52
  • Possible Duplicate: https://stackoverflow.com/questions/35130250/what-is-the-correct-way-to-prevent-caching-on-ajax-calls/35130770 – Twisty Jun 25 '20 at 20:53
  • Thank you for your comments. Reason I didn't think was a duplicate is it is specifically text file based. The other examples are not. I don't know if there is anything specific about the mechanics of returning a text file as opposed to JSON data from a web service. I will try expanding the time frame and report results. – Robert Jun 25 '20 at 21:40
  • There is no difference. It gets interpreted as JSON: *The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string).* – Twisty Jun 25 '20 at 22:30
  • Thank you. In my case what's returned is a URL encoded string. On receipt I URL decode it and the JSON.parse() to an object. For some reason cache false does not produce updated data when the file has clearly been updated and saved. I tried increasing the time interval to 20 seconds per your suggestion. No effect. Is it even possible to achieve what I've laid out here? What am I not getting? Seems like it should be very simple. – Robert Jun 25 '20 at 23:42
  • Read the other posted questions and answers I linked to. If you change the URL each time, it will be a "new" url and won't read from Cache. You may consider switching to `$.getJSON()` if it is a JSON file with proper formatting; it should result in already parsed data. – Twisty Jun 26 '20 at 01:24
  • If you think you're seeing cached data, examine your payload with Network tab and then work to replicate the issue manually without AJAX. You might just be getting the same payload back each time. It's also possible, if the provider is caching static pages, that you're getting the cached server item and not the dynamic content you're expecting. – Twisty Jun 26 '20 at 01:26

0 Answers0