Home  >  Article  >  Web Front-end  >  Several methods to solve the caching problem of jQuery.getJSON

Several methods to solve the caching problem of jQuery.getJSON

巴扎黑
巴扎黑Original
2017-07-03 09:43:361228browse

I was doing testing work today and found a problem that puzzled me. The getJson method of jquery can get the returned result when running on firefox, but when tested on ie8, I found that it was not sent. request, so I couldn't get any returned results. After going through many twists and turns, I found the solution in an online article, so I excerpted it and made some modifications.

1 Make the url different for each call

Method: Add one to the parameter random number.
Example 1:
jQuery.getJSON("$!{Root}/a/a/s.ashx",{ID:"123456",Name: "john",random:Math.random()},function(responseText){}
Example 2:
"xxx.aspx?randID="+Math .random
Example 3:
"xxx.aspx?randID="+ escape(new Date())
2 will cache is set to False
$.ajax does notcacheversion:

$.ajax({
type:"GET"
url:'test.html' ,
cache:false,
dataType:"html",
success:function(msg){
alert(msg);
}
});


3. Add the following statement at the top of the labels.html file:

e57b118234dbe923c36926980f7b4a4e

4ef21fe295e452c3aeb67780abbc5800
4.loadFunction can not only call HTML, but also call script, such as labels.php, you can use the header function in the php file :

b70f4f87e9510961bc22c2b37334b01d
5 Use post instead of get method.
You need to use Post method Note:
Set the Context-Type of the header to application/x-www-form-urlencode to ensure that the server knows that there are parameters in the entity Variables. Usually use XmlHttpRequestObjectSetRequestHeader("Context-Type","application/x-www- form-urlencoded;").

Example: xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

Parameters are key-value pairs with one-to-one name/value correspondence. Each pair of values ​​is separated by an ampersand. For example, var name=abc&sex=man&age=18, pay attention to var name=update. The writing methods of php?abc&sex=man&age=18 and var name=?abc&sex=man&age=18 are both wrong;

The parameters are in the Send (parameter) method Send, for example: xmlHttp.send(name); If it is the get method, directly xmlHttp.send(null);Server sideRequest parametersDistinguish between Get and Post. $username = $_GET["username"]; If it is post mode, then $username = $_POST["username"];

6 Add header("Cache- Control: no-cache, must-reva lidate");
7 Add before sending ajax request xmlHttpRequest.setRequestHeader("If-Modified-Since","0");
8 Add before ajax sends the request xmlHttpRequest.setRequestHeader("Cache-Control","no-cache");

The above is the detailed content of Several methods to solve the caching problem of jQuery.getJSON. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn