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!
My workaround in ASP.NET (pagemethods, webservice, etc.)
I think you have already resolved your issue, but let me share an idea about web caching.
It is true you can add many headers in each language you use, server side, client side, and you can use many other tricks to avoid web caching, but always think that you can never know from where the client are connecting to your server, you never know if he are using a Hotel “Hot-Spot” connection that uses Squid or other caching products.
If the users are using proxy to hide his real position, etc… the real only way to avoid caching is the timestamp in the request also if is unused.
For example:
Then every cache manager you have to pass didnt find the same URL in the cache repository and go re-download the page content.
You can also fix this issue by modifying the jQuery Ajax function by doing the following (as of 1.7.1) to the top of the Ajax function (function starts at line 7212). This change will activate the built-in anti-cache feature of jQuery for all POST requests.
(The full script is available at
http://dl.dropbox.com/u/58016866/jquery-1.7.1.js
.)Insert below line 7221:
Then modify the following (starting at line ~7497).
To:
While my login and signup pages works like a charm in Firefox, IE and Chrome... I've been struggling with this issue in Safari for IOS and OSX, few months ago I found a workaround on the SO.
OR via javascript
This is kinda ugly thing but works for a while.
I don't know why, but returning null to the
onunload
event the page do not get cached in Safari.From my own blog post iOS 6.0 caching Ajax POST requests:
How to fix it: There are various methods to prevent caching of requests. The recommended method is adding a no-cache header. This is how it is done.
jQuery:
Check for iOS 6.0 and set Ajax header like this:
ZeptoJS:
Check for iOS 6.0 and set the Ajax header like this:
Server side
Java:
Make sure to add this at the top the page before any data is sent to the client.
.NET
Or
PHP
This is an update of Baz1nga's answer. Since
options.data
is not an object but a string I just resorted to concatenating the timestamp: