问题描述
我遇到XMLHttpRequest下载渐进式数据的问题。我获得状态2而不是状态3.状态3之后它再也不会被调用。我究竟做错了什么?我读到了需要刷新数据的地方但是我该怎么做?
I'm having issues with XMLHttpRequest downloading progressive data. I get a state 2 and than state 3. After state 3 it never gets called again. What am I doing wrong? I read somewhere I need to flush the data but how do I do that?
这是我的代码:
var xmlHttp = new XMLHttpRequest();
// try to connect to the server
try
{
// initiate server request
xmlHttp.open("GET", "http://208.43.121.133:8164/;", true);
xmlHttp.setRequestHeader("Icy-Metadata", "1");
xmlHttp.onreadystatechange = function()
{
alert("status: "+xmlHttp.status);
alert("State: "+xmlHttp.readyState);
if (xmlHttp.readyState == 3)
{
alert(xmlHttp.responseText);
}
};
xmlHttp.send(null);
}
// display an error in case of failure
catch (e)
{
alert("Can't connect to server:\n" + e.toString());
}
am readyState为3时我是否可以读取xmlHttp.responseText?
am I allowed to read the xmlHttp.responseText when readyState is 3?
推荐答案
Kranu是正确的,当readyState为3时,不允许读取responseText。请参阅
Kranu is correct, you're not allowed to read responseText when readyState is 3. See http://www.davidflanagan.com/2005/08/xmlhttprequestreadystate-3.html
解决方案是一次发送一条消息。当您收到一条消息时,只需再制作一个XHR。这就是谷歌做(服务员?)服务器推送的方式。
The solution is to send a message at a time. When you receive one message, just make another XHR. That's how google does (did?) server push.
这篇关于XMLHttpRequest读取渐进式数据不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!