简体   繁体   中英

jquery - Empty responseText in XMLHttpRequest object when server returns 500

I have to make SOAP calls from javascript between different domains. 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. When using on the same domain the status and the responseText is ok.

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. The allowed headers are "SOAPServer", "SOAPAction" and "Method". I tried it in FF 3.6.10 and 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

To fix this I changed the httpSuccess of jQuery 1.4.2.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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