Solving Ajax page Caching problems __ajax

Source: Internet
Author: User

Reprint an article. The original text is as follows:

Projects sometimes need to use some Ajax effect, because it is simpler, there is no ajax.net to use something like that, the handwriting code is realized. 、

The next day, someone reported me an error; The value of the first read is normal and the following values are not normal; I debugged it, and I did have this problem, and I found out it was the problem with Ajax caching: There are several solutions:

1, in the service End plus header ("Cache-control:no-cache, must-revalidate");(such as PHP)

  2, in the AJAX send the request before adding Anyajaxobj.setrequestheader ("if-modified-since", "0");

  3, in the AJAX send the request before adding Anyajaxobj.setrequestheader ("Cache-control", "No-cache");

  4, after the Ajax URL parameters plus "? fresh=" + math.random (); Of course, the parameter fresh can be arbitrarily taken.

  5, The fifth method is similar to the fourth, after the URL parameter plus "? 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 block of code to prevent caching in the ASP page to be obtained asynchronously:
Response.Buffer =true
Response.ExpiresAbsolute =now ()-1
Response.expires=0
Response.cachecontrol= "No-cache"

3, in the AJAX send the request before adding Xmlhttp.setrequestheader ("if-modified-since", "0"); You can disable caching
Xmlhttp.open ("Get", URL, True);
Xmlhttp.onreadystatechange = callhtml;
Xmlhttp.setrequestheader ("If-modified-since", "0");
Xmlhttp.send ();

Another author wrote:

Ajax cache is maintained by the browser, for a url,ajax sent to the server only on the first request with the server interaction information, after the request, Ajax no longer submit requests to the server, but directly from the cache to extract data.

In some cases, we need to get updated data from the server each time. The idea is to make each request a different URL, without affecting the normal application: After the URL to add random content.
e.g.
url=url+ "&" +math.random ();

Key points:
1. Each request URL is different (Ajax cache will not work)
2. Does not affect the normal application (most basic)

----------------
Method Two: (unconfirmed)
Disable caching in JSP
Response.AddHeader ("Cache-control", "No-cache");
Response.AddHeader ("Expires", "Thu, 1970 00:00:01 GMT");

HTTP:
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache, must-revalidate" >
<meta http-equiv= "Expires" content= "Wed Feb, 1997 08:21:57 GMT" >
<meta http-equiv= "Expires" content= "0" >

Another author wrote:

We all know that Ajax can improve the speed of the page load is the main reason is to reduce the load of duplicate data through Ajax, really do on demand, in this case, we can write Ajax programs to send the Buddha to the west, in the client to do a cache, and further improve data loading speed. Which is to cache data in the browser's memory while loading the data. Once the data is loaded, as long as the page is not refreshed, the data is always cached in memory, when the user view the data again, you do not need to fetch data from the server, greatly reducing the load on the server and improve 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.