如何避免在Internet Explorer 11中使用AJAX缓存,而不需要额外的查询字符串参数或使用POST

时间:2022-01-04 19:40:14

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 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.

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)使用由服务器传递的“cache-control”头。

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.

在许多涉及现代REST API实践的情况下,没有一个答案是充分的或实用的。REST API对于POST和GET请求有完全不同的处理程序,具有完全不同的行为,因此POST通常不是一个合适的或正确的获取替代。此外,许多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?

在这种情况下,客户端上真的没有什么可以做的来防止缓存AJAX GET请求的结果吗?

1 个解决方案

#1


39  

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

#1


39  

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