我正在做一个跨域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");
}
});
}上面的代码在所有浏览器中都工作得很好,但在IE中有时会显示错误,如(aborted)。
为了克服这个错误,我在谷歌中搜索,没有找到任何好的解决方案。
您可以看到显示(aborted)的错误消息。http://postimg.org/image/k01u6t9v5/
当我单独调用特定的URL时,它不会显示任何(已中止)消息(显示成功警告)。但当我进行多个调用时(如图所示),它会显示该类型的错误。
如何克服这个问题?
请帮帮忙
提前感谢
发布于 2013-08-23 07:30:59
我不确定这是不是同样的问题,但在我的例子中,所有这些都需要设置: onerror;onprogress;ontimeout;和onload。以下是一些讨论该问题的参考资料:
还有很多其他的。在他们建议的解决方案中,它们是分散的,有时是相互矛盾的。例如,有人建议将xdr.send调用包装在setTimeout中。
通过为每个事件处理函数添加非空体,我看到的行为消失了。我不确定是否所有这些都是必要的。setTimeout包装器绝对不是必需的。
一条可能不相关的信息:在我的例子中,我决定将每个处理程序绑定到'this‘对象。我还添加了函数实现,以防止我的编译器将它们都赋值给同一个空函数。我的代码是使用GET,而不是POST。YMMV.
您的代码留下了一个未设置的处理程序:
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");
}
});
}https://stackoverflow.com/questions/15786966
复制相似问题