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

时间:2014-04-29 20:29:33

标签: javascript ajax google-chrome xmlhttprequest

我遇到问题,httpReq.responseText在Chrome {33.0.1750.149 m} readyState4后为空console.log(url)。当我在readyState行进行调试并停止时, 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); 是一个空字符串,并在“网络”选项卡中查看,Chrome在“响应”部分显示了一个空的响应。没有什么可以令人惊讶的。

令人惊讶的是,只要我让javascript从该断点继续,“网络”选项卡就会填充响应部分。我知道我的服务器确实返回了数据,而url来自同一个域(localhost)。当我在firefox中测试时,它运行正常。

我的代码如下所示:

.open

有什么想法可能会发生什么?

更新:如果我将httpReq.open('GET', url, false);电话更改为onreadystatechange则可行。 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中的错误吗?

更新:我对此进行了最低限度的重复。我的代码中有两个独立的ajax函数(一个来自我无法控制的模块)。其中一个似乎在做一个同步的http请求。这是一个复制品:

{{1}}

现在我很清楚这是Chrome中的一个错误。我会随身携带一张票。

1 个答案:

答案 0 :(得分:0)

这是Chrome中的一个错误。有关详细信息,请参阅票证:

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

相关问题