jquery's Ajax cache

Source: Internet
Author: User

Recently in the function of a business address book, the function is very simple, my intention is to do data storage and do synchronous update, time reason and the service side of the colleague more inclined to simple point of way, hehe, so we took a real-time query way.

Because the GET request is a bit more, because the page's life cycle is to open the page-close the page, think of the cache, but because the company is using native Ajax, so study how JQ's Ajax cache is written.

The idea of JQ is to detect the need to cache this request, will be in the object pool to detect whether there is a last-modified value of the URL, storage container as follows:

1 // last-modified Header cache for next request 2 lastmodified: {}, 3 etag: {},

Then, when the request is sent to detect if the cache, the following code:

1 //Set the If-modified-since and/or If-none-match header, If in ifmodified mode.2 if(s.ifmodified) {3     if(jquery.lastmodified[Cacheurl]) {4Jqxhr.setrequestheader ("If-modified-since", jquery.lastmodified[Cacheurl]);5     }6     if(jquery.etag[Cacheurl]) {7Jqxhr.setrequestheader ("If-none-match", jquery.etag[Cacheurl]);8     }9}

The code here means that if JQ decides whether to set if-modified-since based on whether the Jquery.lastmodified dictionary contains data. That first request jquery.lastmodified is no data (JS is unable to obtain the browser cache information), so the first request JQXHR is not set if-modified-since, That explains the first request jqxhr.status for 200. Because of the first request, JQ get get to the first request in response to last-modifiedjquery.lastmodified with data, the second request JQXHR will be added if-modified-since header, so Jqxhr.status received the browser directly passed the request response and content 。

And then wait until the request is successful.

1 if(s.ifmodified) {2Modified = Jqxhr.getresponseheader ("last-modified");3     if(modified) {4jquery.lastmodified[Cacheurl] =modified;5     }6Modified = Jqxhr.getresponseheader ("ETag");7     if(modified) {8query.etag[Cacheurl] =modified;9     }Ten}

At this point, get the value of last-modified from the return header, usually a time, the etag is generally a hash, the two methods are similar, then this value will be cached in the JQ pool, the next request is 304.

About LastModified and ETag can refer to the link http://www.51testing.com/html/28/116228-238337.html

jquery's Ajax cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.