[英]Chrome XMLHttpRequest readyState===4 returns empty responseText
I'm having a problem where httpReq.responseText
is empty after readyState
is 4
in Chrome (Version 33.0.1750.149 m). 我在Chrome(Version 33.0.1750.149 m)中的
readyState
为4
后httpReq.responseText
为空的问题。 When I debug through and stop on the console.log(url)
line, readyState
is an empty string, and looking in the Network tab, chrome shows an empty response in the "Response" section for the url. 当我调试通过并在
console.log(url)
行上停止时, readyState
是一个空字符串,然后在“网络”标签中查看,chrome在URL的“响应”部分显示了一个空响应。 Nothing surprising there. 没什么奇怪的。
What's surprising is that as soon as I let the javascript continue from that breakpoint, the Network tab populates the response section. 令人惊讶的是,一旦我让javascript从该断点继续运行,“网络”标签就会填充“响应”部分。 I know for certain my server is returning data, and the url is from the same domain (localhost).
我知道我的服务器肯定在返回数据,并且该URL来自同一域(本地主机)。 When I tested in firefox, it works fine.
当我在Firefox中测试时,它可以正常工作。
My code looks like this: 我的代码如下所示:
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);
Any ideas what might be going on? 任何想法可能会发生什么?
Update : if I change the .open
call to httpReq.open('GET', url, false);
更新 :如果我将
.open
调用更改为httpReq.open('GET', url, false);
it works. 有用。 Something must be going wrong with the
onreadystatechange
function - is this a bug in chrome? onreadystatechange
函数一定出问题了-这是Chrome中的错误吗?
Update : I made a minimal repro of this. 更新 :我对此做了最小的复制。 I have two separate ajax functions in my code (one from a module I don't control).
我的代码中有两个单独的ajax函数(一个来自我无法控制的模块)。 One of them seems to be doing a synchronous http request.
其中之一似乎正在执行同步http请求。 Here's a repro:
这是一个复制品:
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();
Its pretty clear to me now that this is a bug in chrome. 现在我很清楚这是Chrome中的错误。 I'll file a ticket with them.
我将向他们提交罚单。
This is a bug in Chrome. 这是Chrome中的错误。 See the ticket for details:
有关详细信息,请参见票证:
https://code.google.com/p/chromium/issues/detail?id=368444&thanks=368444&ts=1398808430 https://code.google.com/p/chromium/issues/detail?id=368444&thanks=368444&ts=1398808430
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.