XDomainRequest中止POST在IE 9 [英] XDomainRequest aborts POST on IE 9

查看:91
本文介绍了XDomainRequest中止POST在IE 9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做跨域Ajax调用。

I am doing a cross domain Ajax call.

我的code:

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

以上code正常工作在所有的浏览器,但在IE有时它显示类似的错误(中止)。

The above code works fine in all browsers, but in IE sometimes it is showing an error like (aborted).

要克服这种错误我搜索的谷歌并没有找到什么好的解决办法。

To overcome this error I searched in Google and did not find any good solution.

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

You can see the error message where (aborted) is showing. http://postimg.org/image/k01u6t9v5/

当我这样做单独的呼叫到一个特定的URL时,它没有显示任何(中止)消息(显示出成功的警报)。但是,当我做多个呼叫(如图像),它表明类型的错误。

When I do individual call to a specific URL it is not showing any (aborted) message(Showing Success alert). But when I do multiple call (like in the image) its showing that type of error.

如何解决这个问题?

请帮忙

在此先感谢

推荐答案

我不知道这是同样的问题,但对我来说所有这些需要设置:onError的; onprogress; ontimeout;和onload事件。这里有一些参考的讨论这个问题:

I'm not sure this is the same problem, but in my case all of these needed to be set: onerror; onprogress; ontimeout; and onload. Here are some references that discuss the problem:

  • http://social.msdn.microsoft.com/Forums/ie/en-US/30ef3add-767c-4436-b8a9-f1ca19b4812e/ie9-rtm-xdomainrequest-issued-requests-may-abort-if-all-event-handlers-not-specified
  • http://cypressnorth.com/programming/internet-explorer-aborting-ajax-requests-fixed/
  • http://rudovsky.blogspot.com/2012/09/microsoft-shit-xdomainrequest.html
  • https://github.com/faye/faye/pull/98

有一些其他的方面。他们是分散的,有时在他们的建议解决矛盾的。例如,一种建议包裹xdr.send呼叫中一个的setTimeout

There are many others as well. They're scattered and sometimes contradictory in their suggested solution. For example, one suggests wrapping the xdr.send call in a setTimeout.

我所看到的行为走了为每个事件处理函数添加非空机构。我不知道如果所有都是必要的。该setTimeout的包装肯定的没有的必要。

The behavior I was seeing went away by adding non-blank bodies for each of the event handler functions. I'm not sure if all are necessary. The setTimeout wrapper was definitely not necessary.

一个可能无关紧要一块信息:在我的情况,我决定每一个处理程序绑定到这个对象。我还添加了功能的实现,以保持我的编译器从它们分配到同一个空函数。我的code的使用GET,POST没有。情况因人而异。

One possibly irrelevant piece of info: in my case I decided to bind each handler to the 'this' object. I also added function implementations to keep my compiler from assigning them all to the same empty function. My code was using GET, not POST. YMMV.

您code留下一个处理程序取消设置:

Your code leaves one handler unset:

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

这篇关于XDomainRequest中止POST在IE 9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆