当附加查询字符串参数或使用 POST 不是一种选择时,如何避免 Internet Explorer 11 中的 AJAX 缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32261000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to avoid AJAX caching in Internet Explorer 11 when additional query string parameters or using POST are not an option
提问by stolli
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.
我意识到有人问过这个问题,但在现代 REST 实践中,这个问题的先前迭代及其答案都不是准确或充分的。这个问题需要一个明确的答案。
The problem is well known, IE (even 11) caches AJAX requests, which is really really dumb. Everyone understands this.
问题是众所周知的,IE(甚至11)缓存AJAX请求,这真的很愚蠢。每个人都明白这一点。
What is notwell 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:
什么是不容易理解的是,没有对以前的答案是足够的。这个问题在 SO 上的每个先前实例都被标记为由以下任一者充分回答:
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.
1)对每个请求使用唯一的查询字符串参数(如unix时间戳),使每个请求URL唯一,从而防止缓存。
-- or --
- 或者 -
2) using POST instead of GET, as IE does not cache POST requests except in certain unique circumstances.
2) 使用 POST 而不是 GET,因为 IE 不会缓存 POST 请求,除非在某些特殊情况下。
-- or --
- 或者 -
3) using 'cache-control' headers passed by the server.
3) 使用服务器传递的“缓存控制”标头。
IMO in manysituations 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.
IMO 在涉及现代 REST API 实践的许多情况下,这些答案都不够充分或实用。REST API 将具有完全不同的 POST 和 GET 请求处理程序,具有完全不同的行为,因此 POST 通常不是 GET 的合适或正确替代方案。同样,许多 API 对它们进行了严格的验证,并且由于多种原因,当提供它们不期望的查询字符串参数时,会产生 500 或 400 个错误。最后,我们经常与 3rd-party 或其他不灵活的 REST API 接口,在这些 API 中我们无法控制服务器响应提供的标头,并且添加缓存控制标头不在我们的能力范围内。
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?
在这种情况下,在客户端真的没有什么可以阻止 IE 缓存 AJAX GET 请求的结果吗?
回答by Kevin B
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.
当服务器返回内容时,缓存通常通过在内容上设置标头来控制。如果您已经这样做了并且 IE 忽略它们并进行缓存,那么解决它的唯一方法是使用您的问题中提到的缓存破坏技术之一。对于 API,在尝试任何缓存破坏技术之前,最好确保使用正确的缓存标头。
https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching_FAQ
https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching_FAQ
Cache-control: no-cache
Cache-control: no-store
Pragma: no-cache
Expires: 0
回答by Mark M
If you don't control the API, you might be able to disable IE caching by adding request headers on the ajax gets:
如果您不控制 API,您可以通过在 ajax 上添加请求头来禁用 IE 缓存:
'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0'

