Force Cache-Control: no-cache in Chrome via XMLHttpRequest on F5 reload Force Cache-Control: no-cache in Chrome via XMLHttpRequest on F5 reload ajax ajax

Force Cache-Control: no-cache in Chrome via XMLHttpRequest on F5 reload


An alternative would be to append a unique number to the url.

<script>    var xhr = new XMLHttpRequest;    xhr.open('GET', 'test.html?_=' + new Date().getTime());    //xhr.setRequestHeader('Cache-Control', 'no-cache');    xhr.send();</script>

timestamp isn't quite unique, but it should be unique enough for your usecase.


Using a query string for cache control isn't your best option nowadays for multiple reasons, and (only) a few are mentioned in this answer. He even explains the new standard method of version control. Though if you just want to be able to set your request headers, the right way to do it is:

    // via Cache-Control header:    xhr.setRequestHeader("Cache-Control", "no-cache, no-store, max-age=0");        // fallbacks for IE and older browsers:    xhr.setRequestHeader("Expires", "Tue, 01 Jan 1980 1:00:00 GMT");    xhr.setRequestHeader("Pragma", "no-cache"); //Edit: I noticed this is required for Chrome some time ago... forgot to mention here

Hope this helps anyone in the future.


I tried (and failed) some sort of randomization to the URL, but it didn't work because the file I was accessing (.json) was being cached as well.

My solution was to add a timestamp to the call to the json file name (similar approach to ones above, slightly modified). This worked perfectly for me (code snippet below).

doSomething('files/data.json?nocache=' + (new Date()).getTime(), function(text){...

I'm very new at all of this so I'm sure there are reasons this isn't a standard/correct solution, but it worked for me.