Solution to only return cached content when Ajax page parameters are the same
Often use Ajax to write some pages without refreshing content to get the page, this approach is very convenient, but one of the problems is that if the two submitted parameters are the same, the returned content only returns the last obtained content, if we have modified the parameters for the first time, the second call will find that the page has not changed. In this case, the native cache is checked first for Ajax acquisition, and if the native cache already has the same content, the remote server is not accessed. Such operations can improve speed and reduce server pressure. But the drawbacks are also obvious.
To solve this problem. We must add an extra parameter to the Fetch page. The simpler approach is to use a random number.
Examples are as follows
function idCheck() { //参数调用函数
var f = document.modify_form;
var book_num = f.book_num.value;
if(book_num=="") {
window.alert("图书编号不能为空");
f.book_num.focus();
return false;
}
//加一个随机数//////////////////////////////
var number = Math.random();
number = number * 1000000000;
number = Math.ceil(number);
//////////////////////////////////////////
send_request('get_book.php?book_num='+book_num+'&ranum='+number); // 后面的 “ranum=number”是额外加的
}
This avoids the problem of returning the same content to the same parameter page.
There is also a way to add code to the page that is called to prevent this page from being cached
HTM Web page
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache,must-revalidate">
<metahttp-equiv="expires"content="wed,26feb199708:21:57gmt">
Or<metahttp-equiv="expires"content="0">
ASP Web page
response.expires=-1
response.expiresabsolute=now()-1
response.cachecontrol="no-cache"
PHP Web page
header("expires:mon,26jul199705:00:00gmt");
header("cache-control:no-cache,must-revalidate");
header("pragma:no-cache");
JSP Web page
response.addHeader("pragma", "no-cache");
response.addHeader("cache-control", "no-cache,must-revalidate");
response.addHeader("expires", "0");