AJAX頁面參數相同時只返回緩衝的內容的解決辦法

來源:互聯網
上載者:User

AJAX頁面參數相同時只返回緩衝的內容的解決辦法

常利用AJAX寫一些頁面無重新整理的內容擷取頁面,這種方式很快捷也很方便,但其中有一個問題,就是如果兩次提交的參數相同時,返回的內容只返回上次擷取的內容,如果我們在第一次修改了參數,第二次再次調用卻會發現頁面根本沒有改變。這樣的情況是是為AJAX擷取時先檢查本機緩衝,如果本機緩衝已有相同內容,則不訪問遠端伺服器。這樣的操作倒是可以提高速度和減少伺服器壓力。但帶來的弊端也是顯而易見的。

為瞭解決這個問題。我們必須在擷取頁加上一個額外的參數。比較簡單的方法是用一個隨機數。

例子如下

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”是額外加的

}

這樣就可以避免相同參數頁面返回同樣內容的問題了。

還有一種方法為在被調用的頁面中,加入代碼,禁止本頁面被緩衝

htm網頁

<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網頁

response.expires=-1

response.expiresabsolute=now()-1

response.cachecontrol="no-cache"

php網頁

header("expires:mon,26jul199705:00:00gmt");

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

header("pragma:no-cache");

jsp網頁

response.addHeader("pragma", "no-cache");

response.addHeader("cache-control", "no-cache,must-revalidate");

response.addHeader("expires", "0");

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.