当中间为AJAX请求返回的responseXML被切断时,我们遇到一个奇怪的行为。我们正在检查readyState(用于4)和XmlHttp.status(用于200),然后才继续解析responseXML。
这里是相关代码:
.
.
.
if ((myCommunicator != null) && (myCommunicator.XmlHttp.readyState == 4))
{
myCommunicator.OnDataAvailable();
}
OnDataAvailable函数:
SoapCommunicator.OnDataAvailable = function () {
DebugWrite("OnDataAvailable");
XMLResponse = this.XmlHttp.responseXML;
if (this.XmlHttp.status != 200)
{
DebugWrite("XmlHttp.status " + this.XmlHttp.status);
DebugWrite("XmlHttp.statusText " + this.XmlHttp.statusText);
DebugWrite("XmlHttp.readyState " + this.XmlHttp.readyState);
}
// DebugWrite("xml=" + XMLResponse.xml.replace(/\r\n/g, ""));
if (XMLResponse.xml.length > 0)
{
if (0 == XMLResponse.parseError.errorCode)
{
var dataNode = XMLResponse.lastChild.firstChild.firstChild.firstChild;
}
else
{
throw "Failed to parse SOAP response";
}
}
else
{
var result = new Object();
result.IsSuccessful = false;
result.ErrorDescription = "Failed to connect to server.";
result.ErrorCode = failedToConnectToServer;
eval(this.ResultCallBack + "(result)");
} }
在此示例中,dataNode保留信息。将其写入日志时,我们发现有时它会在中间被切断。仅对于大量数据,才注意到此行为。另一件事是,它总是在不同的部分被切断,而不是在精确的X字节之后。
顺便说一句,碰巧的客户使用的是德语编码。
谢谢!
更新:我忘记提及的另一件事是,一旦我们尝试解析数据(在readyState == 4和XmlHttp.status = 200之后),我们就会收到此错误:
错误:消息='完成此操作所需的数据不是 尚可用'
假设您在后端(ASMX)上使用ASP.NET Web服务-尝试将此行添加到web.config文件(至本节):
<httpRuntime maxRequestLength="2147483647" />
我有类似的问题,有时XmlHttpRequest响应被切断。
我不知道这是否是导致您出现问题的原因,但这可以帮助解决此类问题的人。我的问题显然是由响应中包含一些Unicode字符引起的。
似乎是由于Unicode字符,当我在服务器上设置content-length标头字段时,实际上将其设置得太低。当浏览器得到响应时,它仅读取内容长度标头指定的字节数,由于我的错误,该长度指示响应中characters的数目,而不是字节数。] >
我在Node.js上使用String:length来计算content-length的值。显然,它返回字符数,而不是字节数。
通过注释掉我在服务器上设置content-length标头的代码部分,摆脱了这个问题。现在可以正常工作。