這樣的情況是是為AJAX獲取時(shí)先檢查本機(jī)緩存,如果本機(jī)緩存已有相同內(nèi)容,則不訪問(wèn)遠(yuǎn)端服務(wù)器。這樣的操作倒是可以提高速度和減少服務(wù)器壓力。但帶來(lái)的弊端也是顯而易見(jiàn)的。
為了解決這個(gè)問(wèn)題。我們必須在獲取頁(yè)加上一個(gè)額外的參數(shù)。比較簡(jiǎn)單的方法是用一個(gè)隨機(jī)數(shù)。
例子如下
復(fù)制代碼 代碼如下:
function idCheck() { //參數(shù)調(diào)用函數(shù)
var f = document.modify_form;
var book_num = f.book_num.value;
if(book_num=="") {
window.alert("圖書編號(hào)不能為空");
f.book_num.focus();
return false;
}
//加一個(gè)隨機(jī)數(shù)//////////////////////////////
var number = Math.random();
number = number * 1000000000;
number = Math.ceil(number);
//////////////////////////////////////////
send_request('get_book.php?book_num='+book_num+'ranum='+number); // 后面的 “ranum=number”是額外加的
}
這樣就可以避免相同參數(shù)頁(yè)面返回同樣內(nèi)容的問(wèn)題了。
還有一種方法為在被調(diào)用的頁(yè)面中,加入代碼,禁止本頁(yè)面被緩存
htm網(wǎng)頁(yè)
復(fù)制代碼 代碼如下:
metahttp-equiv="pragma"content="no-cache">
metahttp-equiv="cache-control"content="no-cache,must-revalidate">
metahttp-equiv="expires"content="wed,26feb199708:21:57gmt">
或者metahttp-equiv="expires"content="0">
asp網(wǎng)頁(yè)
復(fù)制代碼 代碼如下:
response.expires=-1
response.expiresabsolute=now()-1
response.cachecontrol="no-cache"
php網(wǎng)頁(yè)
復(fù)制代碼 代碼如下:
header("expires:mon,26jul199705:00:00gmt");
header("cache-control:no-cache,must-revalidate");
header("pragma:no-cache");
jsp網(wǎng)頁(yè)
復(fù)制代碼 代碼如下:
response.addHeader("pragma", "no-cache");
response.addHeader("cache-control", "no-cache,must-revalidate");
response.addHeader("expires", "0");