How can I solve the Ajax cache problem?

Source: Internet
Author: User

Projects sometimes use some Ajax effects. Because it is relatively simple, there is no such thing as Ajax.net, And the handwritten code is implemented. 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, this problem exists, it is found that the problem is caused by AJAX cache. There are several solutions:

  1. Add the header ("Cache-Control: no-cache, must-revalidate") on 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 sending a request through ajax ");
  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. Replacing GET with POST: Not recommended

Add a random number:

xmlHttp.open("GET", "ajax.asp?now=" + new Date().getTime(), true);

On the asp page to be obtained asynchronously, write a piece of code to prohibit Caching:

Response.Buffer =TrueResponse.ExpiresAbsolute =Now() - 1Response.Expires=0Response.CacheControl="no-cache"

Add xmlHTTP. setRequestHeader ("If-Modified-Since", "0") before ajax sends a request.

xmlHTTP.open("get", URL, true); xmlHTTP.onreadystatechange = callHTML; xmlHTTP.setRequestHeader("If-Modified-Since","0"); xmlHTTP.send();

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. In subsequent requests, ajax no longer submits requests to the server, instead, extract data directly 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.

url=url+"&"+Math.random();
  1. The URLs of each request are different (the ajax cache does not work)
  2. Does not affect normal applications (most basic)

Disable caching in JSP:

response.addHeader("Cache-Control", "no-cache");response.addHeader("Expires", "Thu, 01 Jan 1970 00:00:01 GMT"); 

We all know that the main reason why ajax can speed up 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.