自从升级到iOS 6以来,我们看到Safari的web视图可以自由缓存$。ajax调用。这是在一个PhoneGap应用程序的上下文中,所以它使用Safari WebView。我们的美元。ajax调用是POST方法,我们有缓存设置为false {cache:false},但这仍然发生。我们尝试手动添加一个时间戳到头部,但它没有帮助。
我们做了更多的研究,发现Safari只返回具有静态函数签名的web服务的缓存结果,并且在每次调用之间不会改变。例如,想象一个函数是这样的:
getNewRecordID(intRecordType)
这个函数一遍又一遍地接收相同的输入参数,但它每次返回的数据应该是不同的。
苹果一定是急于让iOS 6运行得令人印象深刻,他们对缓存设置太满意了。有人在iOS 6上看到过这种行为吗?如果是的话,到底是什么原因造成的呢?
我们发现的解决方法是修改函数签名,像这样:
getNewRecordID(intRecordType, strTimestamp)
然后总是传递一个TimeStamp参数,并在服务器端丢弃这个值。这就解决了这个问题。
我能够通过使用$的组合来解决我的问题。ajaxSetup和追加一个时间戳到我的帖子的url(不是post参数/体)。这是基于之前回答的建议
$(document).ready(function(){
$.ajaxSetup({ type:'POST', headers: {"cache-control","no-cache"}});
$('#myForm').submit(function() {
var data = $('#myForm').serialize();
var now = new Date();
var n = now.getTime();
$.ajax({
type: 'POST',
url: 'myendpoint.cfc?method=login&time='+n,
data: data,
success: function(results){
if(results.success) {
window.location = 'app.cfm';
} else {
console.log(results);
alert('login failed');
}
}
});
});
});
根据应用程序的不同,你可以在iOS 6中使用Safari>Advanced>Web Inspector来解决这个问题,所以这对这种情况很有帮助。
将手机连接到Mac电脑上的Safari浏览器,然后使用开发人员菜单来解决web应用程序的问题。
在更新到iOS6后,清除iPhone上的网站数据,包括使用Web View的特定应用程序。只有一个应用程序有问题,这解决了它在IOS6 Beta测试的方式,从那以后没有真正的问题。
你可能也需要看看你的应用,如果在自定义应用的WebView中,检查NSURLCache。
https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLCache_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003754
我想这取决于你的问题的真正性质,实现等等。
裁判:美元。ajax调用
您还可以通过修改jQuery Ajax函数来解决这个问题,方法是在Ajax函数的顶部执行以下操作(从1.7.1开始)(函数从第7212行开始)。此更改将激活jQuery内置的反缓存功能,用于所有POST请求。
(完整的脚本可以在http://dl.dropbox.com/u/58016866/jquery-1.7.1.js上找到。)
插入到第7221行以下:
if (options.type === "POST") {
options.cache = false;
}
然后修改以下内容(从第~7497行开始)。
if (!s.hasContent) {
// If data is available, append data to URL
if (s.data) {
s.url += (rquery.test(s.url) ? "&" : "?") + s.data;
// #9682: remove data so that it's not used in an eventual retry
delete s.data;
}
// Get ifModifiedKey before adding the anti-cache parameter
ifModifiedKey = s.url;
// Add anti-cache in URL if needed
if (s.cache === false) {
var ts = jQuery.now(),
// Try replacing _= if it is there
ret = s.url.replace(rts, "$1_=" + ts);
// If nothing was replaced, add timestamp to the end.
s.url = ret + ((ret === s.url) ? (rquery.test(s.url) ? "&" : "?") + "_=" + ts : "");
}
}
To:
// More options handling for requests with no content
if (!s.hasContent) {
// If data is available, append data to URL
if (s.data) {
s.url += (rquery.test(s.url) ? "&" : "?") + s.data;
// #9682: remove data so that it's not used in an eventual retry
delete s.data;
}
// Get ifModifiedKey before adding the anti-cache parameter
ifModifiedKey = s.url;
}
// Add anti-cache in URL if needed
if (s.cache === false) {
var ts = jQuery.now(),
// Try replacing _= if it is there
ret = s.url.replace(rts, "$1_=" + ts);
// If nothing was replaced, add timestamp to the end.
s.url = ret + ((ret === s.url) ? (rquery.test(s.url) ? "&" : "?") + "_=" + ts : "");
}
我希望这能对其他在这个问题上绞尽脑汁的开发人员有所帮助。我发现以下任何一个阻止Safari在iOS 6上缓存POST响应:
在请求头中添加[cache-control: no-cache]
添加一个可变URL参数,如当前时间
在响应头中添加[pragma: no-cache]
在响应头中添加[cache-control: no-cache]
我的解决方案是以下在我的Javascript(我所有的AJAX请求是POST)。
$.ajaxSetup({
type: 'POST',
headers: { "cache-control": "no-cache" }
});
我还在许多服务器响应中添加了[pragma: no-cache]标头。
如果您使用上述解决方案,请注意任何设置为global: false的$.ajax()调用都不会使用在$. ajaxsetup()中指定的设置,因此您将需要再次添加头部。