自从升级到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参数,并在服务器端丢弃这个值。这就解决了这个问题。


当前回答

虽然我的登录和注册页面在Firefox, IE和Chrome中工作得很有魅力……我在IOS和OSX的Safari中一直在努力解决这个问题,几个月前我在SO上找到了一个解决方案。

<body onunload="">

或通过javascript

<script type="text/javascript">
window.onunload = function(e){
    e.preventDefault();
    return;
};
</script>   

这是有点丑陋的事情,但有效的一段时间。

我不知道为什么,但是返回null到onunload事件页面不被缓存在Safari中。

其他回答

我在ASP中的解决方案。NET(页面方法,web服务等)

protected void Application_BeginRequest(object sender, EventArgs e)
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
}

您还可以通过修改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 : "");
}

虽然我的登录和注册页面在Firefox, IE和Chrome中工作得很有魅力……我在IOS和OSX的Safari中一直在努力解决这个问题,几个月前我在SO上找到了一个解决方案。

<body onunload="">

或通过javascript

<script type="text/javascript">
window.onunload = function(e){
    e.preventDefault();
    return;
};
</script>   

这是有点丑陋的事情,但有效的一段时间。

我不知道为什么,但是返回null到onunload事件页面不被缓存在Safari中。

从我自己的博客文章iOS 6.0缓存Ajax post请求:

如何修复:有多种方法可以防止缓存请求。推荐的方法是添加一个无缓存头。事情是这样的。

jQuery:

检查iOS 6.0并像这样设置Ajax头:

$.ajaxSetup({ cache: false });

ZeptoJS:

检查iOS 6.0并像这样设置Ajax头:

$.ajax({
    type: 'POST',
    headers : { "cache-control": "no-cache" },
    url : ,
    data:,
    dataType : 'json',
    success : function(responseText) {…}

服务器端

Java:

httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");

确保在将任何数据发送到客户端之前将其添加到页面顶部。

.NET

Response.Cache.SetNoStore();

Or

Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

PHP

header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.

我发现了一个变通办法,这让我很好奇为什么它能起作用。在阅读Tadej关于ASP的回答之前。NET web服务,我试图想出一些可行的方法。

我并不是说这是一个很好的解决方案,我只是想在这里记录一下。

主页:包括JavaScript函数checkStatus()。该方法调用另一个方法,该方法使用jQuery AJAX调用更新html内容。我使用setInterval调用checkStatus()。当然,我遇到了缓存问题。

解决方案:使用另一个页面调用更新。

在主页面上,我设置了一个布尔变量runUpdate,并在body标签中添加了以下内容:

<iframe src="helper.html" style="display: none; visibility: hidden;"></iframe>

在help .html中:

<meta http-equiv="refresh" content="5">
<script type="text/javascript">
    if (parent.runUpdate) { parent.checkStatus(); }
</script>

因此,如果从主页调用checkStatus(),我将获得缓存的内容。如果从子页调用checkStatus,就会得到更新的内容。