簡體   English   中英

在IE9中中止AJAX請求時遇到錯誤

[英]Getting an error when aborting AJAX request in IE9

我在項目中使用長時間輪詢。 當項目正在運行時,長輪詢請求之一就是服務器的等待響應。 現在,當用戶生成新模板時,我必須中止先前的長輪詢調用並運行新模板。

在IE中中止會給出錯誤

couldn't complete the operation due to error c00c023f 

我不想使用Jquery,因此中止先前的請求非常重要。 我嘗試了“嘗試並捕獲”以防止顯示此錯誤,但未解決。

當正常請求(非輪詢請求)請求在幾秒鍾后未到達服務器時,我也使用中止,然后中止舊請求並發送新請求。

我將xmlHttp對象存儲在每個請求的數組中。

如: array_XMLHttp.push(request1);

我像這樣終止它: array_XMLHttp[index_of_request].abort();

我找不到適用於IE9的任何解決方案,盡管它在所有其他瀏覽器上都能完美運行。

更新:

我總是檢查XMLHttp對象的狀態和readyState,如下所示:

function checkResponse(xmlHttp){
    xmlHttp.onreadystatechange=function(){ 
        if(xmlHttp.readyState==4){
            if(xmlHttp.status==200){   
                // Request received
            } 
        }
    }
}      


這是我開始請求的方式

// to start new XmlHttp object
var xmlHttp=crXH();         

// add the request to array to be aborted later if required
array_XMLHttp.push(request1);

// initiate the callback
checkResponse(xmlHttp);

//send the request to server
send(xmlHttp);


當我中止時:

// The problem now the callback function will not read (undefined) the new property added to the aborted object.
array_XMLHttp[index_of_request].aborted = true; 
array_XMLHttp[index_of_request].abort();

謝謝您的幫助!

將我們漫長的討論整理成一個答案...

我在該主題上找到的每一篇類似的文章 (在IE中都不是不常見的問題)中,IE不希望您在調用.abort()之后訪問xmlHttp對象的標准屬性,但它仍會觸發一些onreadystatechange事件,這些事件使您嘗試訪問這些標准屬性,從而導致錯誤。

解決方法是在調用.abort()時在xmlHttp對象上設置您自己的屬性,並在readystatechange處理程序中,首先檢查您自己的.aborted屬性,如果設置了.aborted屬性,則您知道該調用已被中止並且您不要檢查可能導致問題的其他屬性。

因此,如果您的xmlHttp對象稱為x ,則可以這樣做:

x.readystatechange = function() {
    if (!x.aborted) {
        if(xmlHttp.readyState==4){
            if(xmlHttp.status==200){   
                // Request received
            } 
        }
    }
}

而且,當您想中止時:

x.aborted = true;
x.abort();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM