This article is a mirror article of machine translation, please click here to jump to the original article.

View: 12520|Reply: 2

[Jquery] [Turn] to jQuery.getJSON caching problem

[Copy link]
Posted on 9/5/2015 10:21:58 AM | | |
jquery's getJson method reads the same request directly from the cache after the first request to the server to get the returned data, and no longer requests the database.

Here's how to fix it:

1 Make the URL different for each call
Method: Add a random number to the parameter.
Example 1:
jQuery.getJSON("$!{ Root}/a/a/s.ashx",{"ID":id,"Name":name,"Path":path,random:Math.random()},function(responseText){}
Example 2:
"xxx.aspx?randID="+Math.random
Example 3:
"xxx.aspx?randID="+ escape(new Date())
2 Set cache to False
$.ajax uncached version:
$.ajax({
type:"GET"
url:'test.html',
cache:false,
dataType:"html",
success:function(msg){
alert(msg);
}
});
3. Include the following statement at the top of the labels.html file:

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">

<META HTTP-EQUIV="Expires" CONTENT="-1">
4. The load function can not only call HTML, but also script, such as labels.php, you can use the header function in the php file:

<?php

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

?>
5 Use post instead of get method.
When using the Post method, you need to pay attention to:
Set the header's Context-Type to application/x-www-form-urlencode to ensure that the server knows that there are parameter variables in the entity. Usually use the SetRequestHeader("Context-Type","application/x-www- form-urlencoded) of the XmlHttpRequest object; ")。 Example:

xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
The parameter is a key-value pair corresponding to the name/value, and each pair of values is separated by an > sign. For example, var name=abc&sex=man&age=18, pay attention to var name=update.php?

abc&sex=man&age=18 and var name=?abc&sex=man&age=18 are written incorrectly;
The argument is sent in the Send method, e.g. xmlHttp.send(name); If it is a get method, directly xmlHttp.send(null);

The server-side request parameter distinguishes between Get and Post. If it is the get method, then $username = $_GET["username"]; If it is a post method, then $username = $_POST["username"];
6 Add header("Cache-Control: no-cache, must-reva lidate") on the server side;
7 Add xmlHttpRequest.setRequestHeader("If-Modified-Since","0") before ajax sends the request;
8 Add xmlHttpRequest.setRequestHeader("Cache-Control","no-cache") before ajax sends the request;




Previous:c# Convert \u64CD\u4F5C\u6210\u529F to Chinese "Operation Successful"
Next:Validate jQuery html5Validate plugin based on HTML5 forms
 Landlord| Posted on 9/5/2015 10:23:09 AM |
url?username=test&aaa= Math.random()
 Landlord| Posted on 9/5/2015 10:24:24 AM |
$.getJson method When the same page is requested repeatedly, only the data is loaded from the cache, and the URL needs to be followed by dynamic parameters every time it is reloaded
$.getJson('a.ashx?t='+new Date() ,'{a:b}',fun.... )
Changing parameters in the parameter list is invalid.
function changeValidateCode(obj) {
//获取当前的时间作为参数,无具体意义
var timenow = new Date().getTime();
//每次请求需要一个不同的参数,否则可能会返回同样的验证码
This is related to the browser's caching mechanism, and you can also set the page to not cache, so that this parameter is not used.
obj.src='rand.action?d='+timenow;
}
<img id='rand' src='rand.action?t=<%=new Date().getTime() %>' onclick='changeValidateCode(this)' title='Can't see clearly? Change one! ' width='50' height='22'/>
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com