简体   繁体   English

如何禁用 Ajax 缓存?

[英]How to disable Ajax caching?

I am having an issue with ajax caching, This was a problem in IE browser too but i fixed it by Writing the Following code.我遇到了 ajax 缓存问题,这也是 IE 浏览器中的问题,但我通过编写以下代码修复了它。

    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("expires","-1");
    response.setHeader("pragma","no-cache");

But I see Safari4.0 on MAC is Caching the Ajax request(We have a requirment to support this).但是我看到 MAC 上的 Safari4.0 正在缓存 Ajax 请求(我们需要支持这一点)。 Fire Fox never a problem.火狐从来都不是问题。 Regarding this "Expire" i am setting it to -1, i see lot of places it is set 0 or some old date from past.关于这个“过期”,我将它设置为 -1,我看到很多地方将它设置为 0 或过去的某个旧日期。 Will it make a difference?它会有所作为吗?

Send an extra parameter with your GET request that will never be the same, for example, the current timestamp.使用您的 GET 请求发送一个永远不会相同的额外参数,例如,当前时间戳。 Something like:就像是:

url = url + '&nocache=' + new Date().getTime();

This will prevent caching.这将防止缓存。

First, a note on your Expires header.首先,关于您的Expires标题的注释。 Your question doesn't specify what server framework you're using, so I'm not sure if this is applicable.您的问题没有指定您使用的服务器框架,所以我不确定这是否适用。 However, it looks like you might be sending an invalid Expires header.但是,您似乎发送了无效的Expires标头。

TheRFC requires Expires to be a date , however you appear to be setting the header to a literal "-1" . RFC 要求Expires为 date ,但是您似乎将标头设置为文字"-1" There are many frameworks that have an expires property on their HTTP response object that takes an int and automatically calculates a date that is that number of seconds from now.有许多框架在其 HTTP 响应对象上具有 expires 属性,该属性采用 int 并自动计算从现在开始的秒数的日期。

Use a HTTP inspector to ensure that your server is sending a validly formatted date and not -1 in the Expires header.使用HTTP 检查器确保您的服务器发送的是格式正确的日期,而不是Expires标头中的-1


You might try making your Cache-Control header more restrictive:您可以尝试使您的Cache-Control标头更具限制性:

response.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate");

must-revalidate tells caches that they must obey any freshness information you give them. must-revalidate告诉缓存他们必须遵守你给他们的任何新鲜度信息。 HTTP allows caches to serve stale representations under special conditions; HTTP 允许缓存在特殊条件下提供陈旧的表示; by specifying this header, you're telling the cache that you want it to strictly follow your rules.通过指定此标头,您告诉缓存您希望它严格遵守您的规则。 [1] [1]

According to RFC 2616 section 9.5 about POST根据RFC 2616第 9.5 节关于 POST

   Responses to this method are not cacheable, unless the response
   includes appropriate Cache-Control or Expires header fields. However,
   the 303 (See Other) response can be used to direct the user agent to
   retrieve a cacheable resource.

So, the browser must not cache POST responses, unless the response specifies otherwise.因此,除非响应另有说明,否则浏览器不得缓存 POST 响应。 In the same time, browsers may cache GET responses, unless the response specifies otherwise.同时,浏览器可能会缓存 GET 响应,除非响应另有说明。 So, for the requests that should not be cached, such as AJAX requests, POST is preferrable method.所以,对于应该缓存的请求,比如 AJAX 请求,POST 是首选方法。

If you, for any reason, don't want to use POSTs for AJAX, you should use the trick mentioned by minitech , it is in fact widely used to force browser to load current version of any resource.如果因任何原因,不希望使用AJAX的帖子,你应该使用MiniTech移动提到的伎俩,其实它是广泛使用武力浏览器加载任何资源的当前版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM