Analysis and solution to Ajax page cache problems

Source: Internet
Author: User

1. What is the Ajax cache principle?

After Ajax successfully sends the data, it stores the request URL and returned response results in the cache. When Ajax is called to send the same request, it retrieves data directly from the cache to improve the response speed and user experience of the page. Currently, the two request URLs must be identical, including parameters. At this time, the browser will not interact with the server.

II. Benefits of Ajax cache

This design allows the client to make some static page content requests, such as images, css files, and js scripts, faster, faster page response, and saves network communication resources.

III. Ajax cache insufficiency

Although Ajax cache has the above advantages, if Ajax is used to modify some background data, although the data has changed in the background, the page cache has not changed, for the same URL, after Ajax is submitted, the browser simply retrieves data from the cache, which of course won't work.

IV. Solutions to Ajax cache problems

The most effective way to solve this problem is to disable page caching. There are several solutions:

1. Add xmlHttpRequest. setRequestHeader ("Cache-Control", "no-cache") before ajax sends a request ");

2. Add the header on the server ("Cache-Control: no-cache, must-revalidate ");

3. Add xmlHttpRequest. setRequestHeader ("If-Modified-Since", "0 & Prime;) before ajax sends a request ;);

Supplement

4. Add "? Fresh = "+ Math. random (); // of course, the fresh parameter can be any one here

5. The fifth method is similar to the fourth method. Add "? Timestamp = "+ new Date (). getTime ();

6. Replace GET with POST: not recommended


1jQuery provides a method to prevent ajax from using the cache,

<! --

<Script type = "text/javascript" language = "javascript">
$. AjaxSetup ({
Cache: false // close AJAX cache
});
</Script>


The second is to modify the load url.

 

For example, if you add more time parameters to the url, you can use this method again in wap1.0 to avoid reading the cache.

<! --

Function loadEventInfoPage (eventId ){

$. AjaxSetup ({
Cache: true // js and css images on the page loaded after the time is added under the AJAX cache will be reloaded,

// This action will be reloaded, but js, css, and images will be cached.
});
$ ("# ShowEventInfo"). load (ctx + "/custeven TS/viewEvent. action ",
{"Complaint. Id": eventId, "tt" :( new Date (). getTime ()}, function (){})
}


Third, set html cache

<! --

<META HTTP-EQUIV = "Pragma" CONTENT = "no-cache">
<META HTTP-EQUIV = "Cache-Control" CONTENT = "no-cache">
<META HTTP-EQUIV = "Expires" CONTENT = "0">

Related Article

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.