Solve ajax IE cache Problems

Source: Internet
Author: User

I used Ajax to perform a name check. This is valid for the first time. But after the submission, I checked it again. The result is incorrect because of the cache of IE.
Copy codeThe Code is as follows:
Function verify (){
$. Ajax ({
// Issue for IE cache; timestamp = "+ new Date (). getTime ()
Url: "checkGroupName? Timestamp = "+ new Date (). getTime (),
Async: true,
Data: "groupName =" + $ ("# cn "). val () + "& groupTypeForDetail =" + $ ("# groupType "). val () + "& prefix =" + $ ("# p "). val (),
DataType: "html ",
Success: function (data ){
If (data = 1 ){
$ ("# Result" ).html ("<font color = 'green'> Group name [" + $ ("# p "). val () + $ ("# cn "). val () + "] Valid </font> ");
$ ("# Email "). val ($ ("# p "). val () + $ ("# cn "). val () + $ ("# emailHidden "). val ());
$ ('# Subdata'). removeAttr ("disabled ");
} Else if (data = 2 ){
$ ("# Result" ).html ("<font color = 'red'> Group name [" + $ ("# p "). val () + $ ("# cn "). val () + "] already existed. </font> ");
$ ('# Subdata'). attr ('Disabled', "true ");
} Else {
$ ("# Result" example .html ("<font color = 'red'> Group name can not be empty. </font> ");
$ ('# Subdata'). attr ('Disabled', "true ");
}
}
});
}

Principle:
Each request of Firefox will return to the server again to obtain the latest data, but IE will be different. It will cache the previous data, and only the first request will actually read the data from the server, as a result, ajax data will not be updated over time ....
Solution (collected from the Internet):
1. Add the header ("Cache-Control: no-cache, must-revalidate") on the server, or use the following combination to make it better:
Copy codeThe Code is as follows:
Header ("Expires: Sat, 1 Jan 2005 00:00:00 GMT ");
Header ("Last-Modified:". gmdate ("D, d m y h: I: s"). "GMT ");
Header ("cache-Control: no-cache, must-revalidate ");
Header ("Pragma: no-cache ");

2. Add xmlHttpRequest. setRequestHeader ("If-Modified-Since", "0") before ajax sends a request ");
3. Add xmlHttpRequest. 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 fourth method is similar to the third method. Add "? Timestamp = "+ new Date (). getTime (); // This method is recommended. I use this method and I personally think it is more convenient.
6. Replace GET with POST: Not recommended

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.