Home > Web Front-end > JS Tutorial > body text

Several methods to solve the caching problem of jQuery.getJSON

巴扎黑
Release: 2017-07-03 09:43:36
Original
1281 people have browsed it

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);
}
});
Copy after login


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


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 :

header("Cache-Control: no-cache, must-revalidate");

?>
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!