iOS 6 上的 Safari 是否缓存 $.ajax 结果? [英] Is Safari on iOS 6 caching $.ajax results?

查看:31
本文介绍了iOS 6 上的 Safari 是否缓存 $.ajax 结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从升级到 iOS 6 以来,我们看到 Safari 的 web 视图随意缓存 $.ajax 调用.这是在 PhoneGap 应用程序的上下文中,因此它使用的是 Safari WebView.我们的 $.ajax 调用是 POST 方法,我们将缓存设置为 false {cache:false},但这种情况仍在发生.我们尝试将 TimeStamp 手动添加到标题中,但没有帮助.

Since the upgrade to iOS 6, we are seeing Safari's web view take the liberty of caching $.ajax calls. This is in the context of a PhoneGap application so it is using the Safari WebView. Our $.ajax calls are POST methods and we have cache set to false {cache:false}, but still this is happening. We tried manually adding a TimeStamp to the headers but it did not help.

我们进行了更多研究,发现 Safari 仅返回具有静态函数签名且不会随调用而变化的 Web 服务的缓存结果.例如,想象一个函数,调用如下:

We did more research and found that Safari is only returning cached results for web services that have a function signature that is static and does not change from call to call. For instance, imagine a function called something like:

getNewRecordID(intRecordType)

这个函数反复接收相同的输入参数,但每次返回的数据应该都不一样.

This function receives the same input parameters over and over again, but the data it returns should be different every time.

肯定是苹果急于让 iOS 6 快速升级,他们对缓存设置太满意了.有没有其他人在 iOS 6 上看到过这种行为?如果是这样,究竟是什么原因造成的?

Must be in Apple's haste to make iOS 6 zip along impressively they got too happy with the cache settings. Has anyone else seen this behavior on iOS 6? If so, what exactly is causing it?

我们找到的解决方法是将函数签名修改为如下所示:

The workaround that we found was to modify the function signature to be something like this:

getNewRecordID(intRecordType, strTimestamp)

然后总是传入一个 TimeStamp 参数,并在服务器端丢弃该值.这可以解决这个问题.

and then always pass in a TimeStamp parameter as well, and just discard that value on the server side. This works around the issue.

推荐答案

经过一番调查,结果表明 iOS6 上的 Safari 将缓存没有 Cache-Control 标头甚至Cache-Control: max-age"的 POST=0".

After a bit of investigation, turns out that Safari on iOS6 will cache POSTs that have either no Cache-Control headers or even "Cache-Control: max-age=0".

我发现防止这种缓存在全局级别发生而不是必须在服务调用结束时破解随机查询字符串的唯一方法是设置缓存控制:无缓存".

The only way I've found of preventing this caching from happening at a global level rather than having to hack random querystrings onto the end of service calls is to set "Cache-Control: no-cache".

所以:

  • 无 Cache-Control 或 Expires 标头 = iOS6 Safari 将缓存
  • Cache-Control max-age=0 并且立即过期 = iOS6 Safari 将缓存
  • 缓存控制:no-cache = iOS6 Safari 不会缓存

我怀疑 Apple 正在利用第 9.5 节中关于 POST 的 HTTP 规范中的这一点:

I suspect that Apple is taking advantage of this from the HTTP spec in section 9.5 about POST:

对此方法的响应不可缓存,除非响应包括适当的 Cache-Control 或 Expires 头字段.然而,303(见其他)响应可用于引导用户代理到检索可缓存的资源.

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.

所以理论上你可以缓存 POST 响应......谁知道呢.但直到现在,还没有其他浏览器制造商认为这是一个好主意.但是当没有设置 Cache-Control 或 Expires 标头时,这不考虑缓存,只有当有一些设置时.所以它一定是一个错误.

So in theory you can cache POST responses...who knew. But no other browser maker has ever thought it would be a good idea until now. But that does NOT account for the caching when no Cache-Control or Expires headers are set, only when there are some set. So it must be a bug.

下面是我在我的 Apache 配置的正确位中使用的内容来定位我的整个 API,因为碰巧我实际上并不想缓存任何东西,甚至获取.我不知道如何仅为 POST 设置它.

Below is what I use in the right bit of my Apache config to target the whole of my API because as it happens I don't actually want to cache anything, even gets. What I don't know is how to set this just for POSTs.

Header set Cache-Control "no-cache"

更新:刚刚注意到我没有指出只有当 POST 相同时,所以更改任何 POST 数据或 URL 就可以了.所以你可以像其他地方提到的那样,在 URL 中添加一些随机数据或一些 POST 数据.

Update: Just noticed that I didn't point out that it is only when the POST is the same, so change any of the POST data or URL and you're fine. So you can as mentioned elsewhere just add some random data to the URL or a bit of POST data.

更新:如果您希望在 Apache 中这样,您可以将无缓存"限制为 POST:

Update: You can limit the "no-cache" just to POSTs if you wish like this in Apache:

SetEnvIf Request_Method "POST" IS_POST
Header set Cache-Control "no-cache" env=IS_POST

这篇关于iOS 6 上的 Safari 是否缓存 $.ajax 结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆