Search code examples
javascriptxdr

XDomainRequest sometimes randomly aborts


XDomainRequest most of them time works ok but sometimes aborts in ie9 specially. has anyone experienced this before?

In case you want to see this is the xdr implementation im using:

(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 );

Solution

  • I had this issue a while back and I found that if you wrap your send method inside a setTimeout it solves the issue.

    setTimeout(function(){
        xdr.send( ( s.hasContent && s.data ) || null );
    }, 0);