簡體   English   中英

如何使用jquery將ajax響應存儲在瀏覽器緩存中

[英]How to store the ajax response in browser cache with jquery

我正在嘗試將ajax響應存儲在瀏覽器緩存中。 我知道僅將其存儲為(example.com)這樣的URL,但是想存儲具有(例如example.com?xyz=abc%20efg&mno=hjk )這樣的URL的數據。

我的代碼:

beforeSend: function () {
                var addC=url;          // I want to add data also with this url
                console.log(addC);
                if (localCache.exist(addC)) {
                    printRes(localCache.get(url));
                    console.log("cached values");
                    return false;
                }
                return true;
            },

var localCache = {
    data: {},
    remove: function (url) {
        delete localCache.data[url];
    },
    exist: function (url) {
        return localCache.data.hasOwnProperty(url) && localCache.data[url] !== null;
    },
    get: function (url) {
        console.log('Getting in cache for url' + url);
        return localCache.data[url];
    },
    set: function (url, cachedData, callback) {
        localCache.remove(url);
        localCache.data[url] = cachedData;
        if ($.isFunction(callback)) callback(cachedData);
    }
};

請任何人幫我解決這個問題。

通過閱讀MDN中的文檔 ,我認為您做錯了。 如果要在ajax調用中將ajax響應CACHE屬性緩存為true ,還請注意它提供了來自服務器的正確緩存過期標頭。 也[來自MDN]

切勿使用傳統的GET參數(例如other-cached- page.html?parameterName = value)訪問緩存的文件。 這將使瀏覽器繞過緩存並嘗試從網絡獲取它。 要鏈接到具有用JavaScript解析的參數的緩存資源,請使用鏈接的哈希部分中的參數,例如other-cached-page.html#whatever?parameterName = value。

如果您正在尋找本地存儲[鍵值存儲]

globalStorage['cache_1'].setItem("key",data);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM