XDomainRequest在IE 9上中止POST

use*_*138 5 ajax cross-domain internet-explorer-9 xdomainrequest

我正在进行跨域Ajax调用.

我的代码:

if (window.XDomainRequest) // Check whether the browser supports XDR.
{
    xdr = new XDomainRequest(); // Create a new XDR object.
    if (xdr) {
        xdr.timeout = 3000;//Set the timeout time to  3 second.
        xdr.onload = function () {
            alert("Success");
        };
        xdr.onerror = function () {
            alert("Error");
        };
        xdr.ontimeout = function () {
            alert("Error");
        };
        xdr.open("post", urlSearch);
        xdr.send();
    }
}
else {
    $.ajax({
        url: urlSearch,
        type: 'POST',
        dataType: 'json',
        timeout: 3000,
        success: function (data) {
            alert("Success");
        },
        error: function () {
            alert("Error");
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

上面的代码在所有浏览器中都可以正常工作,但在IE中,它有时会显示错误(中止).

为了克服这个错误,我在谷歌搜索并没有找到任何好的解决方案.

您可以看到显示(中止)的错误消息. http://postimg.org/image/k01u6t9v5/

当我对特定URL进行单独调用时,它不会显示任何(已中止)消息(显示成功警报).但是当我做多次调用时(比如在图像中)它显示出那种类型的错误.

如何克服这个问题?

请帮忙

提前致谢

Ric*_*aca 9

我不确定这是同一个问题,但在我的情况下,所有这些都需要设置:onerror; onprogress; ontimeout; 和onload.以下是一些讨论问题的参考资料:

还有很多其他的.它们在建议的解决方案中是分散的,有时是矛盾的.例如,建议在setTimeout中包装xdr.send调用.

我看到的行为通过为每个事件处理函数添加非空白主体而消失了.我不确定是否有必要.绝对没有必要使用setTimeout包装器.

一个可能不相关的信息:在我的情况下,我决定将每个处理程序绑定到'this'对象.我还添加了函数实现,以防止我的编译器将它们全部分配给相同的空函数.我的代码使用的是GET,而不是POST.因人而异.

您的代码未设置一个处理程序:

if (window.XDomainRequest) // Check whether the browser supports XDR.
{
    xdr = new XDomainRequest(); // Create a new XDR object.
    if (xdr) {
        xdr.timeout = 3000;//Set the timeout time to  3 second.
        xdr.onload = function () {
            alert("Success");
        };
        xdr.onerror = function () {
            alert("Error");
        };
        xdr.ontimeout = function () {
            alert("Error");
        };
        // this also needs to be set
        xdr.onprogress = function() {
            window.console.log('progress');
        };
        xdr.open("post", urlSearch);
        xdr.send();
    }
}
else {
    $.ajax({
        url: urlSearch,
        type: 'POST',
        dataType: 'json',
        timeout: 3000,
        success: function (data) {
            alert("Success");
        },
        error: function () {
            alert("Error");
        }
    });
}
Run Code Online (Sandbox Code Playgroud)