How can I avoid caching data when loading via XMLHttpRequest ()

I am writing an extension for the Chrome browser (and later I hope for a port in Firefox). The extension downloads the configuration file from my server - an XML file through XMLHttpRequest. I find that it downloads the file once, and each subsequent call just seems to use the cached original version of the file. It doesn’t matter if I modify the file on the server.

I read that you can try

xmlhttp.setRequestHeader ('Pragma', 'Cache-Control: no-cache');

and so I did it, but it doesn't seem to make any difference. The only way to get a new file is to delete the browser cache, which obviously is not a solution for my growing users.

This seems like a problem I wouldn't be the first person to deal with, so since thatcacheing rules seem to support this as a policy that cannot be easily avoided, my question is, what is the best design? is there any best practice that i don't know about? Should I push somehow, not pull?

+4
source share
1 answer

A simple way is to add a useless parameter containing the request time. As time tends to move back and forth, you can be sure that your request is unique and therefore will not be cached.

For example (if the URL is in the url string):

 url += '?_time=' + (new Date()).getTime(); 

or, if your URL already has query parameters,

 url += '&_time=' + (new Date()).getTime(); 
+4
source

All Articles