iOS 6上的Safari是否缓存$ .ajax结果?

本文翻译自:Is Safari on iOS 6 caching $.ajax results?

Since the upgrade to iOS 6, we are seeing Safari's web view take the liberty of caching $.ajax calls. 自从升级到iOS 6之后,我们看到Safari的Web视图可以缓存$.ajax调用。 This is in the context of a PhoneGap application so it is using the Safari WebView. 这是在PhoneGap应用程序的上下文中,因此它正在使用Safari WebView。 Our $.ajax calls are POST methods and we have cache set to false {cache:false} , but still this is happening. 我们的$.ajax调用是POST方法,并且我们将缓存设置为false {cache:false} ,但这仍然在发生。 We tried manually adding a TimeStamp to the headers but it did not help. 我们尝试将TimeStamp手动添加到标题中,但没有帮助。

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. 我们进行了更多研究,发现Safari仅返回具有静态功能签名且不会随调用而变化的Web服务的缓存结果。 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. 一定要赶紧Apple加快iOS 6的速度,他们对缓存设置太满意了。 Has anyone else seen this behavior on iOS 6? 有人在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. 然后也总是传入一个TimeStamp参数,并在服务器端丢弃该值。 This works around the issue. 这可以解决此问题。 I hope this helps some other poor soul who spends 15 hours on this issue like I did! 希望这对其他像我一样在这个问题上花费15个小时的可怜人有所帮助!


#1楼

参考:https://stackoom.com/question/qTc9/iOS-上的Safari是否缓存-ajax结果


#2楼

I just had this issue as well in a PhoneGap application. 我在PhoneGap应用程序中也遇到了这个问题。 I solved it by using the JavaScript function getTime() in the following manner: 我通过以下方式使用JavaScript函数getTime()了该问题:

var currentTime = new Date();
var n = currentTime.getTime();
postUrl = "http://www.example.com/test.php?nocache="+n;
$.post(postUrl, callbackFunction);

I wasted a few hours figuring this out. 我浪费了几个小时来解决这个问题。 It would have been nice of Apple to notify developers of this caching issue. Apple最好将这个缓存问题通知开发人员。


#3楼

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". 经过一番调查,结果发现iOS6上的Safari将缓存没有Cache-Control标头甚至没有“ Cache-Control:max-age = 0”的POST。

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:no-cache”。

So: 所以:

  • No Cache-Control or Expires headers = iOS6 Safari will cache 没有Cache-Control或Expires标头= iOS6 Safari将缓存
  • Cache-Control max-age=0 and an immediate Expires = iOS6 Safari will cache 缓存控制max-age = 0,并且立即到期= iOS6 Safari将会缓存
  • Cache-Control: no-cache = iOS6 Safari will NOT cache 缓存控制:无缓存= iOS6 Safari无法缓存

I suspect that Apple is taking advantage of this from the HTTP spec in section 9.5 about POST: 我怀疑Apple从9.5节有关POST的HTTP规范中利用了这一点:

Responses to this method are not cacheable, unless the response includes appropriate Cache-Control or Expires header fields. 除非响应包含适当的Cache-Control或Expires标头字段,否则对此方法的响应不可缓存。 However, the 303 (See Other) response can be used to direct the user agent to retrieve a cacheable resource. 但是,303(请参阅其他)响应可用于指导用户代理检索可缓存的资源。

So in theory you can cache POST responses...who knew. 因此,从理论上讲,您可以缓存POST响应...谁知道。 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. 但是,仅当设置了Cache-Control或Expires头时,才考虑缓存。 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. 以下是在我的Apache配置的正确位置使用的全部我的API,因为发生这种情况时,我实际上并不想缓存任何东西,甚至不需要缓存任何东西。 What I don't know is how to set this just for POSTs. 我不知道如何仅针对POST设置它。

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. 更新:只是注意到我没有指出它只是在POST相同的情况下,因此更改任何POST数据或URL都可以。 So you can as mentioned elsewhere just add some random data to the URL or a bit of POST data. 因此,您可以像在其他地方提到的那样,仅向URL中添加一些随机数据或一些POST数据。

Update: You can limit the "no-cache" just to POSTs if you wish like this in Apache: 更新:如果您希望在Apache中这样,可以将“无缓存”仅限制为POST:

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

#4楼

I had the same problem with a webapp getting data from ASP.NET webservice 我从web应用程序获取ASP.NET Web服务的数据时遇到了同样的问题

This worked for me: 这对我有用:

public WebService()
{
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    ...
}

#5楼

Simple solution for all your web service requests, assuming you're using jQuery: 假设您使用的是jQuery,则适用于所有Web服务请求的简单解决方案:

$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
    // you can use originalOptions.type || options.type to restrict specific type of requests
    options.data = jQuery.param($.extend(originalOptions.data||{}, { 
      timeStamp: new Date().getTime()
    }));
});

Read more about the jQuery prefilter call here . 在此处阅读有关jQuery prefilter调用的更多信息。

If you aren't using jQuery, check the docs for your library of choice. 如果您不使用jQuery,请检查文档以查找您选择的库。 They may have similar functionality. 它们可能具有类似的功能。


#6楼

Depending on the app you can trouble shoot the issue now in iOS 6 using Safari>Advanced>Web Inspector so that is helpful with this situation. 根据不同的应用程序,您现在可以使用Safari>高级> Web检查器在iOS 6中解决问题,因此在这种情况下很有帮助。

Connect the phone to Safari on a Mac an then use the developer menu to trouble shoot the web app. 将手机连接到Mac上的Safari,然后使用开发人员菜单对网络应用进行故障排除。

Clear the website data on the iPhone after update to iOS6, including specific to the app using a Web View. 更新到iOS6后,清除iPhone上的网站数据,包括使用Web View特定于该应用程序的数据。 Only one app had an issue and this solved it during IOS6 Beta testing way back, since then no real problems. 只有一个应用存在问题,此问题在IOS6 Beta测试期间得以解决,从那时起就没有真正的问题。

You may need to look at your app as well, check out NSURLCache if in a WebView in a custom app. 您可能还需要查看您的应用程序,如果在自定义应用程序的WebView中,请检出NSURLCache。

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLCache_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003754 https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLCache_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003754

I guess depending on the true nature of your problem, implementation, etc. .. 我想这取决于您的问题,实施等的真实性质。

Ref: $.ajax calls 参考:$ .ajax调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值