【问题标题】:jquery - Empty responseText in XMLHttpRequest object when server returns 500jquery - 服务器返回 500 时 XMLHttpRequest 对象中的空 responseText
【发布时间】:2010-10-14 14:54:20
【问题描述】:

我必须在不同域之间通过 javascript 进行 SOAP 调用。在服务器端,有一个允许的域、方法和标头列表,这些域、方法和标头都包含在过滤器的响应中。当响应代码为 200 但在服务器端引发异常时,它运行良好(即使在不同域之间),xhr 对象的状态为 0 而不是 500 并且 responseText 为空。在同一个域上使用时,状态和响应文本都可以。

相关代码如下:

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。允许的标头是“SOAPServer”、“SOAPAction”和“Method”。 我在 FF 3.6.10 和 Google Chrome 7.0.517.36 中尝试过

【问题讨论】:

    标签: javascript jquery exception soap


    【解决方案1】:

    FF 3.6.8 返回 xhr.status === 0 如果服务器返回 HTTP 代码,如果 301。修复需要更改 $.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;
    },
    

    【讨论】:

    • 感谢您的回复,但恐怕行不通(我试过了)。从日志中可以看出服务器响应为 500,我也可以在 firebug 中看到。只是实际响应(即异常)在某处丢失。与此同时,我用原生调用(不涉及 jquery)构建了一个简单的 javascript,并得到了相同的结果。似乎在跨域资源共享的情况下,如果状态不是 200,浏览器不会转发响应。我在 flex 中实现客户端时发现了一篇关于此问题的文章。
    • 我知道这不是同一种语言/大小写,但问题的根源可能是相同的。
    • 你说你在做跨域请求吗? $.ajax 除非你做 JSONP,否则不能跨域工作。
    • 您可能会在服务器上生成异常。尝试捕获它,然后将其格式化为 xml。发回给客户。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多