ext.ajax.request:ajax調用過程中,千萬不要把request覆蓋了.

來源:互聯網
上載者:User

今天調了一天的錯誤,非常鬱悶,直到晚上才發現,原來是頁面中定義的XMLHttpRequest對象是個全域對象,但有兩個ajax調用都引用了它,每隔幾分鐘就會出現問題,還是莫名其妙的問題,我以為是資料解析有問題或後台問題,調啊調,結果是兩個ajax調用,第二個調用了同一個request,也就是覆蓋了第一個的,害得第一個偶爾會得到第二個的值,導致解析出錯.......
以後每個ajax調用一定不要用全域的變數儲存request.而是臨時產生,然後將request綁定它的到處理上.
function test(){
var temp_request = createXMLHttpRequest();
deal_change.request = temp_request;
temp_request.onreadystatechange=deal_change;
temp_request.open("GET",url,true);
temp_request.setRequestHeader("If-Modified-Since","0"); //不緩衝Ajax
temp_request.send(null);
}
function deal_change()
{
var rStr;
try{
if(deal_change.request.readyState==4)\\
通過deal_change.request引用調用它的request
{
if(deal_change.request.status==200)
{
rStr = trim(deal_change.request.responseText);
...................
}}}
切記切記.
當然若使用 架構就不用這麼麻煩了. 本文連結http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20120721/32053.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.