【发布时间】:2013-10-09 10:46:17
【问题描述】:
我必须为我的 phonegap 应用程序缓存 json 数据 10 分钟,该怎么做? 服务器响应已经带有过期标头。
Cache-Control: max-age=315360000
Expires: Sun, 12 Sep 2038 20:15:20 GMT
但是 jquery ajax 请求没有被缓存。
【问题讨论】:
标签: json http jquery caching cordova
我必须为我的 phonegap 应用程序缓存 json 数据 10 分钟,该怎么做? 服务器响应已经带有过期标头。
Cache-Control: max-age=315360000
Expires: Sun, 12 Sep 2038 20:15:20 GMT
但是 jquery ajax 请求没有被缓存。
【问题讨论】:
标签: json http jquery caching cordova
[a] 有时我们需要为浏览器启用和禁用 ajax 请求缓存。可以通过下面的标志来完成。 缓存:真
如果设置为 true,ajax 请求将根据内容沉积标头开始缓存。
如果设置为 false,则会将唯一时间戳附加到请求中,因此 该请求永远不会被缓存。 http://www.exp.com/api/get_posts/?count=12&page=1&_=1381305201264
代码:
global_xhrAbort = $.ajax({
cache: true,
type: "GET",
timeout: 30000,
async: false,
url: finalurl,
data: null,
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
dataType: "json",
complete: function () {
},
success: function (data) {
console.log('picked from server koimoi: success');
Page_topstoriesJson = GetJSONifNeeded(data); ;
HTMLSTORAGE_SET('landingpage', GetJSONstringfyifNeeded(data)); //will expire in ten mintues
doChangesForMainandTopStoriesSlider();
HideLoading();
}
,
error: function (errmsg) {
alert_dialog('Request failed. Please check your internet connection or Press Refresh Button.',true);
console.log('index_devicreReadyError: ' + errmsg);
}
});
Jquery AJAX 缓存文档:(默认值:true,对于 dataType 'script' 和 'jsonp' 为 false) 类型:布尔值 如果设置为 false,它将强制浏览器不缓存请求的页面。注意:将缓存设置为 false 仅适用于 HEAD 和 GET 请求。它通过将“_={timestamp}”附加到 GET 参数来工作。其他类型的请求不需要该参数,除非在 IE8 中对已由 GET 请求的 URL 进行 POST。
[b] 记住:chrome 上的 Ctrl+R 总是从服务器加载新数据,即使它被缓存了。在新窗口中打开页面以查看测试结果。
【讨论】: