Prevent JQuery Ajax Request from Caching In IE

In this article I will explain how we can Prevent JQuery Ajax Request from Caching In IE(Internet Explorer).

You may noticed that only Internet Explorer by default caches the AJAX request, no other browser, by default, caches AJAX requests… but Internet explorer does and when you make second AJAX request with same parameters what internet explorer does instead creating another request,  it’s smartly returns the result  from cache which had saved from first request in order to increase the speed and we can not clear the cache with ctrl + f5 it does not work in IE.

Instead, we have to clear the cache from developer mode

 

That will clear the cache from page but that annoying that every time we have to clear the cache in order to see latest data, so of course we can’t go with this option. so now the original question is how we can get rid of cache problem in IE?

How to get rid of cache problem in IE

Well the solution is quite simple as I told earlier that IE returns the result from the cache if its noticed the same URL with same parameters, so we can fix this problem via adding unique value in query string.

1. Disable the cache for all AJAX request 

First solution is disable the cache using jQuery for all AJAX requests, by disabling the cache in jQuery request you will notice that there will be a unique value in query string that is timestamp which would be append in the querystring . Here is the syntax that will add unique value for all of your ajax request for that page only  where  you will have included this syntax, you can disable caching globally using $.ajaxSetup(), for example:

 $.ajaxSetup({ cache: false });

2. Disable the cache for a particular request

If you don’t want to disable the request for all AJAX requests then you can also disable for a particular request instead using the above syntax, you can call one simple property in $.ajax() method mentioned below:

 $.ajax({
       cache: false,
      //other options...
  });

Now you’ll be very curious to test this but remember you’ll have to clear the old cache from the page using developer mode as I mentioned earlier.
Hope, you will like this

Leave a Reply