Introduction to several methods of preventing Ajax caching

Source: Internet
Author: User

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

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, here the parameters fresh can be arbitrarily taken ";

5, fifth method and fourth similar, after the URL parameter plus "" "timestamp=" +new Date (). GetTime (); " ;

6, with Post instead of get: not recommended.

1. Add a random number:

The code is as follows Copy Code
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:

The code is as follows Copy Code
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:

The code is as follows Copy Code
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, such as:

The code is as follows Copy Code
url=url+ "&" +math.random ();


Key points:

1, each request URL is not the same (Ajax cache will not work);

2, does not affect the normal application (the most basic).

Method Two (unconfirmed):

Disable Caching in PHP

The code is as follows Copy Code
Header ("Cache-control:no-cache, must-revalidate");

Disable caching in JSP

The code is as follows Copy Code
Response.AddHeader ("Cache-control", "No-cache");
Response.AddHeader ("Expires", "thu,01 1970 00:00:01 GMT");


HTTP:

The code is as follows Copy Code
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache,must-revalidate" >
<meta http-equiv= "Expires" content= "wed,26 Feb 1997 08:21:57 GMT" >
<meta http-equiv= "Expires" content= "0" >

The above methods of prohibiting AJAX data caching are just a few of the things we used to do, such as php,asp.net and so on, are not tested, so don't take it out.

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.