XDomainRequest 大多数时候工作正常但有时在 ie9 中特别中止。以前有人经历过吗?
如果您想查看这是我正在使用的 xdr 实现:
(function( jQuery ) {
if ( window.XDomainRequest ) {
jQuery.ajaxTransport(function( s ) {
if ( s.crossDomain && s.async ) {
if ( s.timeout ) {
s.xdrTimeout = s.timeout;
delete s.timeout;
}
var xdr;
return {
send: function( _, complete ) {
function callback( status, statusText, responses, responseHeaders ) {
xdr.onload = xdr.onerror = xdr.ontimeout = jQuery.noop;
xdr = undefined;
complete( status, statusText, responses, responseHeaders );
}
xdr = new window.XDomainRequest();
xdr.onload = function() {
callback( 200, "OK", { text: xdr.responseText }, "Content-Type: " + xdr.contentType );
};
xdr.onerror = function() {
callback( 404, "Not Found" );
};
xdr.onprogress = function() {};
if ( s.xdrTimeout ) {
xdr.ontimeout = function() {
callback( 0, "timeout" );
};
xdr.timeout = s.xdrTimeout;
}
xdr.open( s.type, s.url, true );
xdr.send( ( s.hasContent && s.data ) || null );
},
abort: function() {
if ( xdr ) {
xdr.onerror = jQuery.noop();
xdr.abort();
}
}
};
}
});
}
})( jQuery );
最佳答案
我前阵子遇到过这个问题,我发现如果将 send 方法包装在 setTimeout 中就可以解决问题。
setTimeout(function(){
xdr.send( ( s.hasContent && s.data ) || null );
}, 0);
关于javascript - XDomainRequest 有时会随机中止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26414831/