1

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 );
makeitmorehuman
  • 11,287
  • 3
  • 52
  • 76

1 Answers1

2

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);
  • 1
    My guess would be xdr.open() is incorrectly executing asynchronously for some reason. putting send in a timeout puts the send method at the end of the queue of scopes to execute, thus pushing it after whatever .open is doing. But this is of course all speculation. (and probably butchery of terms) – Kevin B Oct 16 '14 at 22:28