是Safari浏览器在iOS 6缓存$阿贾克斯的结果吗? [英] Is Safari on iOS 6 caching $.ajax results?

查看:225
本文介绍了是Safari浏览器在iOS 6缓存$阿贾克斯的结果吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于升级到iOS 6,我们看到的Safari浏览器的Web视图下缓存的自由 $。阿贾克斯通话。这是在一个PhoneGap的应用程序,以便在使用的Safari web视图的上下文中。我们的 $ AJAX 电话是 POST 方法,我们必须缓存设置为false {缓存:假} ,但仍发生这种情况。我们试图手动添加时间戳的头,但它并没有帮助。

我们做更多的研究和发现,Safari浏览器只返回缓存的结果具有函数签名是静态的,不会从电话改为调用Web服务。例如,假设一个名为类似的功能:

  getNewRecordID(intRecordType)
 

此功能在接收到相同的输入参数,一遍又一遍,但它返回的数据应该每次都不同。

必须在苹果的仓促做出的iOS 6拉链以及IM pressively他们得到了太多高兴的缓存设置。有其他人看到在iOS 6这种行为?如果是的话,究竟是什么造成的呢?


这是我们找到的解决方法是修改函数签名是这样的:

  getNewRecordID(intRecordType,strTimestamp)
 

然后一直传递一个时间戳参数为好,只需放弃在服务器端的价值。它解决的问题。我希望这可以帮助其他一些可怜的灵魂谁花15个小时这个问题像我一样!

解决方案

在进行位的调查,事实证明,Safari浏览器上的iOS6​​将缓存有要么没有的Cache-Control头,甚至缓存控制帖数:最大年龄= 0。

我发现了preventing这种缓存发生在全球层面上,而不是砍随机查询字符串上的服务呼叫结束的唯一方法是设置的Cache-Control:no-cache的<。 / P>

所以:

  • 没有的Cache-Control或Expires头= iOS6 Safari浏览器会缓存
  • 缓存控制最大年龄= 0,并立即过期= iOS6 Safari浏览器会缓存
  • 的Cache-Control:no-cache的= iOS6 Safari浏览器将不缓存

我怀疑苹果是从HTTP规范服用本节9.5的优势有关POST:

  

该方法的响应不能缓存,除非响应      包括适当的Cache-Control或Expires头字段。然而,      的303(见其他)响应可以用来指示用户代理      检索缓存的资源。

所以理论上可以缓存POST响应......谁知道。但是,没有其他的浏览器厂商曾经认为这将是一个好主意,直​​到如今。但是,这并不占缓存时没有缓存控制或Expires头设置,只有当有一些集。因此,它必须是一个错误。

下面是我用我的Apache配置的权位为目标的全我的API,因为它发生了,我实际上并不想缓存什么,甚至会。我不知道的是如何设置这个刚刚上岗。

 报头组的Cache-Control无缓存
 

更新:只注意到我并没有指出,这是只有在POST是一样的,所以改变任何POST数据,或URL和你的罚款。所以,你可以为其他地方提到的只是一些随机数据添加到URL还是有点POST数据。

更新:您可以限制的无缓存刚刚上岗,如果你想这样在Apache中:

  SetEnvIf之后REQUEST_METHODPOSTIS_POST
头集缓存控制无缓存ENV = IS_POST
 

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.

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.

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)

and then always pass in a TimeStamp parameter as well, and just discard that value on the server side. This works around the issue. I hope this helps some other poor soul who spends 15 hours on this issue like I did!

解决方案

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

So:

  • No Cache-Control or Expires headers = iOS6 Safari will cache
  • Cache-Control max-age=0 and an immediate Expires = iOS6 Safari will cache
  • Cache-Control: no-cache = iOS6 Safari will NOT cache

I suspect that Apple is taking advantage of this from the HTTP spec in section 9.5 about 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 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.

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"

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.

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

这篇关于是Safari浏览器在iOS 6缓存$阿贾克斯的结果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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