Ajax cache Solution

Source: Internet
Author: User

Ajax cache solution repostedArticle, The original text is as follows:

Projects sometimes use some Ajax effects, because it is relatively simple, there is no such thing as ajax.net, hand writingCodeThis is achieved.

The next day, someone reported an error to me, saying that only the value read for the first time is normal, and the subsequent values are not normal;

After debugging, I found out that the problem was caused by Ajax cache: There are several solutions:

1. Add the header ("cache-control: No-cache, must-revalidate") to the server (such as in PHP)

2. Add anyajaxobj. setRequestHeader ("If-modified-since", "0") before Ajax sends a request ");

3. Add anyajaxobj. setRequestHeader ("cache-control", "No-Cache") before Ajax sends a request ");

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

1. Add a random number XMLHTTP. Open ("get", "Ajax. asp? Now = "+ new date (). gettime (), true );

2. Write a forbidden cache code on the ASP page to be obtained asynchronously: response. buffer = true response. expiresabsolute = now ()-1 response. expires = 0 response. cachecontrol = "no-Cache"

3. Add XMLHTTP before Ajax sends a request. setRequestHeader ("If-modified-since", "0"); Disable XMLHTTP caching. open ("get", URL, true); XMLHTTP. onreadystatechange = callhtml; XMLHTTP. setRequestHeader ("If-modified-since", "0"); XMLHTTP. send ();

Another author wrote: the Ajax cache is maintained by the browser. For a URL sent to the server, Ajax only interacts with the server during the first request, ajax does not submit requests to the server, but directly extracts data from the cache. In some cases, we need to obtain the updated data from the server every time. The idea is to make the URLs of each request different without affecting the normal application: Add random content after the URL. E.g. url = URL + "&" + math. random (); key points: 1. the URLs of each request are different (Ajax cache does not work. does not affect normal applications (most basic )----------------

Method 2: (unconfirmed) Disable response caching in JSP. addheader ("cache-control", "No-Cache"); response. addheader ("expires", "Thu, 01 Jan 1970 00:00:01 GMT"); http:


another author writes: we all know that, the main reason why AJAX can increase the speed of page loading is that it reduces the loading of duplicate data through Ajax and truly achieves on-demand acquisition. In this case, when writing an Ajax Program , we may send it to the west and cache it again on the client side to further increase the data loading speed. That is, when loading data, the data is cached in the browser memory. Once the data is loaded, the data is always cached in the memory as long as the page is not refreshed, when you view the data again, you do not need to obtain the data from the server, which greatly reduces the server load and improves the user experience.

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.