首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XDomainRequest有时会随机中止

XDomainRequest有时会随机中止
EN

Stack Overflow用户
提问于 2014-10-16 22:17:22
回答 1查看 101关注 0票数 1

XDomainRequest大部分时间工作正常,但有时在ie9中会中止。以前有人经历过这种事吗?

如果您希望看到这是使用以下方法的xdr实现im:

代码语言:javascript
复制
(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 );
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-16 22:19:55

前一段时间我遇到了这个问题,我发现如果您将发送方法包装在setTimeout中,它就解决了这个问题。

代码语言:javascript
复制
setTimeout(function(){
    xdr.send( ( s.hasContent && s.data ) || null );
}, 0);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26414831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档