ajax緩存有好處,但也有壞處,緩存有時候會導(dǎo)致誤操作,影響用戶體驗(yàn),若你的WEB項(xiàng)目不需要ajax緩存功能,可按下述方法來禁止ajax緩存。
一、在ASP中禁止ajax緩存:
'放在ASP網(wǎng)頁最開頭部分
Response.expires=0Response.addHeader("pragma","no-cache")Response.addHeader("Cache-Control","no-cache, must-revalidate")
二、在PHP中禁止Ajax緩存:
//放在PHP網(wǎng)頁開頭部分header("Expires: Thu, 01 Jan 1970 00:00:01 GMT");header("Cache-Control: no-cache, must-revalidate");header("Pragma: no-cache");
三、在JSp中禁止ajax緩存:
//放在JSP網(wǎng)頁最開頭部分response.addHeader("Cache-Control", "no-cache");response.addHeader("Expires", "Thu, 01 Jan 1970 00:00:01 GMT");
四、通過給網(wǎng)頁添加隨機(jī)字符強(qiáng)制更新:如
var url = 'http://url/';url += '?temp=' + new Date().getTime();url += '?temp=' + Math.random();
五、若是靜態(tài)HTML,可添加HTTP headers頭禁止緩存,比如:
<meta http-equiv="pragma" content="no-cache" /><meta http-equiv="Cache-Control" content="no-cache, must-revalidate" /><meta http-equiv="expires" content="Thu, 01 Jan 1970 00:00:01 GMT" /><meta http-equiv="expires" content="0" />
六、可以在XMLHttpRequest發(fā)送請求之前加上以下代碼禁止ajax緩存:
XMLHttpRequest.setRequestHeader("If-Modified-Since","0");XMLHttpRequest.send(null);
七、jQuery ajax Load禁止
在jQuery提供一個防止ajax使用緩存的方法,把下面的語句加在head的javascript文件里,就可以解決問題。
$.ajaxSetup ({ cache: false //關(guān)閉AJAX相應(yīng)的緩存 });
小結(jié),不過現(xiàn)在都是使用jquery ajax了我們?nèi)绻幌M彺婵梢灾苯釉O(shè)置 cache: false 這樣可以解決post ,get等提交數(shù)據(jù)方式哦。
新聞熱點(diǎn)
疑難解答
圖片精選