javascript - jquery - 当服务器返回 500 时,XMLHttpRequest 对象中的响应文本为空

标签 javascript jquery exception soap

我必须在不同域之间从 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/

相关文章:

java - 当前一个语句失败时确保语句执行的干净方法

javascript - 使用提交表单填充表(javascript)

javascript - 在javascript中关闭缓存

javascript - 简单的 JavaScript Chrome 扩展 - 打开 URL - 从输入文本修改

javascript - 两个不同的 html 表格突出显示相同的行顺序

jquery - 从下拉列表中复制选项列表。查询

javascript - Ajax 在登录表单中显示错误而不重新加载页面

json - Symfony 3 API REST - try catch JSON 响应格式的异常

php - Smarty IE9 请求下载 index.php 文件(在没有上传之后)?是我的代码有问题还是 activecollab?

c++ - 文件流析构函数可以在 C++ 中抛出异常吗?