How to avoid AJAX caching in Internet Explorer 11

2019-01-31 10:52发布

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?

1条回答
够拽才男人
2楼-- · 2019-01-31 11:17

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
查看更多
登录 后发表回答