Is Safari on iOS 6 caching $.ajax results?

前端 未结 25 966
轮回少年
轮回少年 2020-11-22 09:34

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

相关标签:
25条回答
  • 2020-11-22 10:15

    A quick work-around for GWT-RPC services is to add this to all the remote methods:

    getThreadLocalResponse().setHeader("Cache-Control", "no-cache");
    
    0 讨论(0)
  • 2020-11-22 10:16

    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:

    if (options.type === "POST") {
        options.cache = false;
    }
    

    Then modify the following (starting at line ~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 : "");
    }
    
    0 讨论(0)
  • 2020-11-22 10:16

    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.

    Connect the phone to Safari on a Mac an then use the developer menu to trouble shoot the web app.

    Clear the website data on the iPhone after update to iOS6, including specific to the app using a Web View. Only one app had an issue and this solved it during IOS6 Beta testing way back, since then no real problems.

    You may need to look at your app as well, check out NSURLCache if in a WebView in a custom app.

    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

    0 讨论(0)
  • 2020-11-22 10:18

    I had the same problem with a webapp getting data from ASP.NET webservice

    This worked for me:

    public WebService()
    {
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        ...
    }
    
    0 讨论(0)
  • 2020-11-22 10:18

    Things that DID NOT WORK for me with an iPad 4/iOS 6:

    My request containing: Cache-Control:no-cache

    //asp.net's:
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache)
    

    Adding cache: false to my jQuery ajax call

     $.ajax(
            {
                url: postUrl,
                type: "POST",
                cache: false,
                ...
    

    Only this did the trick:

    var currentTime = new Date();
    var n = currentTime.getTime();
    postUrl = "http://www.example.com/test.php?nocache="+n;
    $.post(postUrl, callbackFunction);
    
    0 讨论(0)
  • 2020-11-22 10:18

    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.

    <body onunload="">
    

    OR via javascript

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

    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.

    0 讨论(0)
提交回复
热议问题