Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

javascript - 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个小时的可怜人有所帮助!)

  ask by user1684978 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...