jQuery的缓存阿贾克斯XMLHtt presponse使用本地存储 [英] Jquery Caching Ajax XMLHttpResponse using Local Storage

查看:183
本文介绍了jQuery的缓存阿贾克斯XMLHtt presponse使用本地存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有必要升级到提供本地存储的缓存能力,目标是首先使每一个Ajax请求检查是否有一个缓存的响应HTML,那么就不会调用服务器端一个jQuery的网站,而是从获取数据高速缓存代替。 一切都准备好与高速缓存,并显示从缓存中的数据等。

I have a Jquery web site that needed to be upgraded to provide Local Storage caching capability, Target was making every Ajax request checks first if there is a cached response HTML, then it will NOT call the server side, but gets data from cache instead. Everything going OK with caching and displaying data from cache and so on..

我的问题出现;看来,当我尝试缓存来自Ajax调用使用JSON.Stringify()序列化,当然之后的XMLHtt presponse对象返回时,desearialized对象从JSON.Parse()返回不包含响应头!

My problem appeared when applying my caching core to sites that heavily depending on XML HTTP Response Headers; It Appeared that when I try to cache the XMLHttpResponse object returned from ajax call after serializing it of course using JSON.Stringify(), the desearialized object returned from JSON.Parse() DOESN'T CONTAIN RESPONSE HEADERS!

我必须从包含所有previously添加页眉缓存返回XMLHtt presponse对象,因为所有的网站都用它处理为XMLHtt presponse的对象。

I have to return an XMLHttpResponse object from cache containing all previously added headers, since all sites are dealing with it as an object of XMLHttpResponse.

任何想法?

推荐答案

我找到了一个工作,围绕这个...

I found a work around for this...

由于所有的网站需要XMLHtt presponse对象来处理,我建立像自定义XMLHtt presponse类,这个类包含了所有的变量/被用在这些网站的功能,如下面的例子:

Since All Sites need an XMLHttpResponse object to deal with, I built something like a Custom XMLHttpResponse class, this class contains all variables/functions being used in those sites, like the below example:

var CustomXMLHTTPResponse =
{
    ResponseHeadersArr: new Array(),

    getResponseHeader: function (Key) {
        return this.ResponseHeadersArr[Key];
    }
.
.
}

和代替缓存整个对象,我只缓存什么,我需要从原来的XMLHtt presponse对象,其实我缓存中的所有响应头在这种情况下,当我从缓存中这个数据,我建立一个新的CustomXMLHTT presponse对象,然后从缓存中得到的数据填充它,最后我通过CustomXMLHTT presponse对象。

And instead of caching the whole object, I only cache what I need from the original XMLHttpResponse object, actually I cache All Response Headers in this case; And when I get this data from cache, I build a new CustomXMLHTTPResponse object, then fill it with data gotten from cache, and finally I pass the CustomXMLHTTPResponse object.

所以,当函数getResponseHeader()在CustomXMLHTT presponse类被调用时,它会起到相同的getResponseHeader()在原XMLHTT presponse类。

So, When function getResponseHeader() in CustomXMLHTTPResponse class being called, it would act the same as getResponseHeader() in the original XMLHTTPResponse class.

这篇关于jQuery的缓存阿贾克斯XMLHtt presponse使用本地存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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