如何为半动态数据(即JSON)启用Ajax请求缓存? [英] How to Enable ajax request caching for semi dynamic data (ie JSON)?

查看:210
本文介绍了如何为半动态数据(即JSON)启用Ajax请求缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为phonegap应用程序缓存json数据10分钟,该怎么做? 服务器响应已经带有到期标头.

I have to cache json data for my phonegap applicaiton for 10 minutes how to do that? server response is already with the expiry headers.

Cache-Control:  max-age=315360000
Expires:    Sun, 12 Sep 2038 20:15:20 GMT

但未缓存jquery ajax请求.

BUT jquery ajax request is not being cached.

推荐答案

[a] 有时我们需要为浏览器启用和禁用ajax请求缓存.可以通过下面的标志来完成. 缓存:true

[a] Sometimes we need to enable and disable ajax request caching for browsers. can be done via below flag. cache: true

  • 如果设置为true,则ajax请求将根据内容沉积标头开始缓存.

  • if set to true ajax request will start caching depending upon the content deposition header.

如果设置为false,则唯一时间戳会附加到请求中,因此 该请求永远不会被缓存. http://www.exp.com/api/get_posts/?count = 12& page = 1& __ = 1381305201264

if set to false a unique timestamp will be appended to request so that request is never cached. 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请求一起正常使用.它通过在GET参数后面附加"_ = {timestamp}"来工作.对于其他类型的请求,则不需要此参数,但在IE8中,当对GET已经请求的URL进行POST时,该参数就不会出现.

Jquery AJAX cache documentation : (default: true, false for dataType 'script' and 'jsonp') Type: Boolean If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.

[b] 切记:chrome上的Ctrl + R始终从服务器加载新数据,即使已缓存也是如此.在新窗口中打开页面,以在测试时查看结果.

[b] Remember: Ctrl+R on chrome always loads new data from server, even if its cached. Open page in new window to see the results while testing.

这篇关于如何为半动态数据(即JSON)启用Ajax请求缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆