当为 AJAX 请求返回的 responseXML 在中间被 chop 时,我们遇到了一个奇怪的行为。我们正在检查 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 保存信息。将它写入日志时,我们看到有时它会在中间被切断。只有在大量数据时才会注意到此行为。关于它的另一件事是它总是在不同的部分被 chop ,而不是在精确的 X 字节之后。
顺便说一句,发生这种情况的客户使用的是德语编码。
谢谢!
更新: 我忘记提及的另一件事是,一旦我们尝试解析数据(在 readyState == 4 和 XmlHttp.status = 200 之后),我们就会收到此错误:
ERROR: Message='The data necessary to complete this operation is not yet available'
最佳答案
假设您在后端 (ASMX) 使用 ASP.NET Web 服务 - 尝试将此行添加到 web.config 文件(到部分):
<httpRuntime maxRequestLength="2147483647" />
关于javascript - AJAX 响应 - XmlHttp.responseXML 被切断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15134804/