Stop Google chrome from caching AJAX get requests.

Google chrome caches the AJAX get requests and we get unusual behaviour if we add logic based on the AJAX response. This only happens for AJAX get requests. Browser never caches the posts requests.

The issue

We had a redirect based on the AJAX response and the should have changed but Google chrome was caching the response and we were getting the same response for every AJAX get requests. We got into the redirection loop.

The solution

We can stop Google chrome to stop AJAX get requests in multiple ways. Following are some ways to do this:

Using timestamp in the AJAX call parameter

We can add the date or timestamp to the AJAX call to stop browser cache like below:

$.get('/getdata?_=' + new Date().getTime(), function(data) {
    console.log(data); 
});

By setting http header

Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0

By setting cache:false

$.ajax({ 
   type: "GET",
   cache: false, 
});
Share