Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid AJAX caching in Internet Explorer 11 when additional query string parameters or using POST are not an option

I realize this question has been asked, but in modern REST practice none of the previous iterations of this question nor their answers are accurate or sufficient. A definitive answer to this question is needed.

The problem is well known, IE (even 11) caches AJAX requests, which is really really dumb. Everyone understands this.

What is not well understood is that none of the previous answers are sufficient. Every previous instance of this question on SO is marked as sufficiently answered by either:

1) Using a unique query string parameter (such as a unix timestamp) on each request, so as to make each request URL unique, thereby preventing caching.

-- or --

2) using POST instead of GET, as IE does not cache POST requests except in certain unique circumstances.

-- or --

3) using 'cache-control' headers passed by the server.

IMO in many situations involving modern REST API practice, none of these answers are sufficient or practical. A REST API will have completely different handlers for POST and GET requests, with completely different behavior, so POST is typically not an appropriate or correct alternative to GET. As well, many APIs have strict validation around them, and for numerous reasons, will generate 500 or 400 errors when fed query string parameters that they aren't expecting. Lastly, often we are interfacing with 3rd-party or otherwise inflexible REST APIs where we do not have control over the headers provided by the server response, and adding cache control headers is not within our power.

So, the question is:

Is there really nothing that can be done on the client-side in this situation to prevent I.E. from caching the results of an AJAX GET request?

like image 410
stolli Avatar asked Aug 27 '15 23:08

stolli


People also ask

How do I stop ajax from caching?

ajax, which will allow you to turn caching off: $. ajax({url: "myurl", success: myCallback, cache: false});

Does browser cache ajax requests?

Fact #1 : Ajax Caching Is The Same As HTTP Caching At this level, the browser doesn't know or care about Ajax requests. It simply obeys the normal HTTP caching rules based on the response headers returned from the server. If you know about HTTP caching already, you can apply that knowledge to Ajax caching.

Are ajax calls cached?

Although we can use a standard caching solution provided by HTTP (yes, Ajax is cached by HTTP), there is a catch: It works for GET requests only (not POST). Furthermore, you cannot control cache expiration on the application side.

What does cache false do in ajax?

The cache: false is used by developers to prevent all future AJAX requests from being cached, regardless of which jQuery method they use.


2 Answers

Caching is normally controlled through setting headers on the content when it is returned by the server. If you're already doing that and IE is ignoring them and caching anyway, the only way to get around it would be to use one of the cache busting techniques mentioned in your question. In the case of an API, it would likely be better to make sure you are using proper cache headers before attempting any of the cache busting techniques.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching_FAQ

Cache-control: no-cache Cache-control: no-store Pragma: no-cache Expires: 0 
like image 121
Kevin B Avatar answered Sep 23 '22 17:09

Kevin B


If you don't control the API, you might be able to disable IE caching by adding request headers on the ajax gets:

'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0' 
like image 22
Mark M Avatar answered Sep 24 '22 17:09

Mark M