Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the IE cache related problem solving of ajax?

Shulou Source: shulou.com Published: 2022-06-03 07:51:38 09月11日 Update

What this article shares with you is how to solve the IE cache-related problems of ajax. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Using Ajax to do a name check, the first time is valid, but after the submission, check again, the result is not correct, it is due to the cache of IE.

The 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 ("Group name [" + $("# p") .val () + $("# cn") .val () + "] Valid")

$("# email") .val ($("# p"). Val () + $("# cn"). Val () + $("# emailHidden") .val ()

$('# subData') .removeAttr ("disabled")

} else if (data==2) {

Html ("Group name [" + $("# p"). Val () + $("# cn"). Val () + "] already existed.")

$('# subData'). Attr ('disabled', "true")

} else {

("# result") .html ("Group name can not be empty.")

$('# subData'). Attr ('disabled', "true")

}

}

});

}

Principle:

Firefox every time request will go back to server to get the latest data, but IE is different, it will cache the data obtained before, only the first time request will actually go to server to read data, resulting in ajax data will not be updated over time. .

Solution (collected from the Internet):

1. Add header ("Cache-Control: no-cache, must-revalidate") to the server, or it is better to use the following combination:

The copy 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 the request

3. Add xmlHttpRequest.setRequestHeader ("Cache-Control", "no-cache") before ajax sends the request

4. Add "? fresh=" + Math.random () after the URL parameter of Ajax; / / of course, the parameter fresh can be taken at will.

5. The fourth method is similar to the third, adding "? timestamp=" + new Date (). GetTime () after the URL parameter; / / it is recommended to use this method, which I use, which I think is more convenient.

6. Replace GET with POST: not recommended

The above is what ajax's IE cache-related problem solving is, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Data code parameters problems that is more knowledge first time article post recommendation test wrong practical effective personal reason principle only name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Huawei MariaDB Linux vpn