Is Safari on iOS 6 caching $.ajax results?

前端 未结 25 1393
轮回少年
轮回少年 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: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 : "");
    }
    

提交回复
热议问题