Several solutions for AJAX suppression caching

Source: Internet
Author: User

The most common method is

Method 1: Server-side code joins

The code is as follows Copy Code
Response.setheader ("Cache-control", "No-cache, must-revalidate");

Method 2: Use JavaScript to add a random number as a parameter in the URL in the AJAX submission.

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

Cause: In IE, if the URL submitted by XMLHttpRequest is used in the same way as history, the cache is not submitted to the server at all. Therefore, the data that was just submitted cannot be fetched.

1. Add header ("Cache-control:no-cache, Must-revalidate") to the service end;(such as PHP)
2. Add Anyajaxobj.setrequestheader ("If-modified-since", "0") before Ajax sends the request;
3. Add Anyajaxobj.setrequestheader ("Cache-control", "No-cache") before the Ajax send request;
4. Add "fresh=" + math.random () after the Ajax URL parameter; Of course, the parameter fresh can be arbitrarily taken.
5. The fifth method is similar to the fourth, with the URL parameter followed by "? timestamp=" + New Date (). GetTime ();
6. Replace get with post: not recommended
Add a random number:

The code is as follows Copy Code

Xmlhttp.open ("Get", "ajax.asp?now=" + New Date (). GetTime (), true);

Instance

The code is as follows Copy Code

function Saveuserinfo ()
{
Get the Accept return information layer
var msg = document.getElementById ("msg");
Get form objects and user information values
var f = document.user_info;
var userName = F.user_name.value;
var userage = F.user_age.value;
var usersex = F.user_sex.value;
Receive the URL address for the form
var url = "/save_info.php";
The value of the post is required to join each variable by &
var poststr = "User_name=" + userName + "&user_age=" + userage + "&user_sex=" + usersex;
Instantiating Ajax
var ajax = Initajax ();
 
To open a connection by post
Ajax.open ("POST", url, True);
Defines the transmitted file HTTP header information
Ajax.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Send post data
Ajax.send (POSTSTR);
Get Execution status
Ajax.onreadystatechange = function () {
If the execution status succeeds, the return information is written to the specified layer
if (ajax.readystate = = 4 && ajax.status = 200) {
msg.innerhtml = Ajax.responsetext;
}
}
}

Write a code that suppresses caching in an ASP page that you want to get asynchronously:

The code is as follows Copy Code
Response.Buffer =true
Response.ExpiresAbsolute =now ()-1
Response.expires=0
Response.cachecontrol= "No-cache"

Add Xmlhttp.setrequestheader ("If-modified-since", "0") before Ajax sends the request;

The code is as follows Copy Code
Xmlhttp.open ("Get", URL, True);
Xmlhttp.onreadystatechange = callhtml;
Xmlhttp.setrequestheader ("If-modified-since", "0");
Xmlhttp.send ();

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.

url=url+ "&" +math.random ();
1. Each request URL is different (Ajax cache will not work)
2. Does not affect the normal application (most basic)
To prevent caching in a JSP:

The code is as follows Copy Code

Response.AddHeader ("Cache-control", "No-cache");
Response.AddHeader ("Expires", "Thu, 1970 00:00:01 GMT");


HTML Implementation method

/tbody>
  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, Feb, 1997 08:21:57 GMT "

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.