简体   繁体   English

jquery - 当服务器返回 500 时,XMLHttpRequest 对象中的 responseText 为空

[英]jquery - Empty responseText in XMLHttpRequest object when server returns 500

I have to make SOAP calls from javascript between different domains.我必须在不同域之间从 javascript 进行 SOAP 调用。 On the server side there is a list of allowed domains, methods and headers which are included in the response by a filter.在服务器端,有一个包含在过滤器响应中的允许域、方法和标头的列表。 It works well (even between different domains) when the response code is 200 but when an exception is thrown on the server side the xhr object has 0 status instead of 500 and the responseText is empty.当响应代码为 200 时(即使在不同域之间),它也能很好地工作,但是当服务器端抛出异常时,xhr 对象的状态为 0 而不是 500,并且 responseText 为空。 When using on the same domain the status and the responseText is ok.在同一个域上使用时,状态和 responseText 没问题。

The relevant code is as follows:相关代码如下:

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);
    }
});

I'm using jQuery-1.4.2.我正在使用 jQuery-1.4.2。 The allowed headers are "SOAPServer", "SOAPAction" and "Method".允许的标头是“SOAPServer”、“SOAPAction”和“Method”。 I tried it in FF 3.6.10 and Google Chrome 7.0.517.36我在 FF 3.6.10 和 Google Chrome 7.0.517.36 中尝试过

FF 3.6.8 returns an xhr.status === 0 when server replies with HTTP code if 301. The fix requires changeing httpSuccess function of $.ajax FF 3.6.8 在服务器回复 HTTP 代码时返回 xhr.status === 0 如果 301。修复需要更改 $.ajax 的 httpSuccess 函数

To fix this I changed the httpSuccess of jQuery 1.4.2.为了解决这个问题,我更改了 jQuery 1.4.2 的httpSuccess

original:原来的:

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;
},

modified:修改的:

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;
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM