簡體   English   中英

Chrome XMLHttpRequest readyState === 4返回空的responseText

[英]Chrome XMLHttpRequest readyState===4 returns empty responseText

我在Chrome(Version 33.0.1750.149 m)中的readyState4httpReq.responseText為空的問題。 當我調試通過並在console.log(url)行上停止時, readyState是一個空字符串,然后在“網絡”標簽中查看,chrome在URL的“響應”部分顯示了一個空響應。 沒什么奇怪的。

令人驚訝的是,一旦我讓javascript從該斷點繼續運行,“網絡”標簽就會填充“響應”部分。 我知道我的服務器肯定在返回數據,並且該URL來自同一域(本地主機)。 當我在Firefox中測試時,它可以正常工作。

我的代碼如下所示:

    var httpReq = new XMLHttpRequest();

    httpReq.onreadystatechange = function() {
        if( httpReq.readyState === 4 ) {
            if( httpReq.status === 200 ) {
                console.log(url)
                result.return(httpReq.responseText)
            } else {
                if(!result.isResolved)
                    result.throw(new Error('Error in request'))
            }
        }
    }

    httpReq.open('GET', url, true);
    httpReq.send(null);

任何想法可能會發生什么?

更新 :如果我將.open調用更改為httpReq.open('GET', url, false); 有用。 onreadystatechange函數一定出問題了-這是Chrome中的錯誤嗎?

更新 :我對此做了最小的復制。 我的代碼中有兩個單獨的ajax函數(一個來自我無法控制的模塊)。 其中之一似乎正在執行同步http請求。 這是一個復制品:

var url = '/'
var httpReq = new XMLHttpRequest();
var httpReq2 = new XMLHttpRequest();

httpReq.onreadystatechange = function() {
    console.log('a '+httpReq.readyState+":"+httpReq.status+' - '+httpReq.responseText.substring(0,40))
};
httpReq2.onreadystatechange = function() {
    console.log('b '+httpReq.readyState+":"+httpReq.status+' - '+httpReq.responseText.substring(0,40))
};

httpReq.open('GET', url ); // asynchronous *first*
httpReq.send();
httpReq2.open('GET', url, false); // synchronous *second*
httpReq2.send();

現在我很清楚這是Chrome中的錯誤。 我將向他們提交罰單。

這是Chrome中的錯誤。 有關詳細信息,請參見票證:

https://code.google.com/p/chromium/issues/detail?id=368444&thanks=368444&ts=1398808430

暫無
暫無

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

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