我必须在不同域之间从 javascript 进行 SOAP 调用。在服务器端有一个允许的域、方法和 header 的列表,它们包含在过滤器的响应中。当响应代码为 200 时,它运行良好(即使在不同的域之间),但当服务器端引发异常时,xhr 对象的状态为 0 而不是 500,并且响应文本为空。当在同一域上使用时,状态和响应文本正常。
相关代码如下:
function onError(xhr, status, thrownError) {
alert(xhr.status);
alert(xhr.responseText);
}
$.ajax({
type: "POST",
url: SOAPClient.Proxy,
dataType: "xml",
processData: false,
data: content,
context: context,
contentType : SOAPClient.ContentType + "; " + SOAPClient.CharSet,
error: onError,
success: onSuccess,
complete: onComplete,
beforeSend: function(req) {
req.setRequestHeader("Method", "POST");
req.setRequestHeader("Content-Length", SOAPClient.ContentLength);
req.setRequestHeader("SOAPServer", SOAPClient.SOAPServer);
req.setRequestHeader("SOAPAction", soapReq.Action);
}
});
我正在使用 jQuery-1.4.2。允许的 header 为“SOAPServer”、“SOAPAction”和“Method”。 我在FF 3.6.10和Google Chrome 7.0.517.36中尝试过
最佳答案
当服务器回复 HTTP 代码(如果为 301)时,FF 3.6.8 返回 xhr.status === 0。修复需要更改 $.ajax 的 httpSuccess 函数
为了解决这个问题,我更改了 jQuery 1.4.2 的 httpSuccess
。
原文:
httpSuccess: function( xhr ) {
try {
// IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
return !xhr.status && location.protocol === "file:" ||
// Opera returns 0 when status is 304
( xhr.status >= 200 && xhr.status < 300 ) ||
xhr.status === 304 || xhr.status === 1223 || xhr.status === 0;
} catch(e) {}
return false;
},
修改:
httpSuccess: function( xhr ) {
try {
// IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
return !xhr.status && location.protocol === "file:" ||
// Opera returns 0 when status is 304
( xhr.status >= 200 && xhr.status < 300 ) ||
xhr.status === 304 || xhr.status === 1223 ||
( xhr.status === 0 && xhr.statusText.toUpperCase() === 'OK');
} catch(e) {}
return false;
},
关于javascript - jquery - 当服务器返回 500 时,XMLHttpRequest 对象中的响应文本为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3934514/