X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fajax%2Fxhr.js;h=a6473dd87a61557a7b0aff4f24ce74f40b01ec42;hb=ea3e10a49207f76957b5bd87095634882d5d374b;hp=b18274c437eff9488880ee2672960effe60ae5b4;hpb=d6fbbe1080fdcaf8eb22753eddf000aeb7d99545;p=jquery.git diff --git a/src/ajax/xhr.js b/src/ajax/xhr.js index b18274c..a6473dd 100644 --- a/src/ajax/xhr.js +++ b/src/ajax/xhr.js @@ -1,5 +1,22 @@ (function( jQuery ) { +var // #5280: next active xhr id and list of active xhrs' callbacks + xhrId = jQuery.now(), + xhrCallbacks, + + // XHR used to determine supports properties + testXHR; + +// #5280: Internet Explorer will keep connections alive if we don't abort on unload +function xhrOnUnloadAbort() { + jQuery( window ).unload(function() { + // Abort all pending requests + for ( var key in xhrCallbacks ) { + xhrCallbacks[ key ]( 0, 1 ); + } + }); +} + // Functions to create xhrs function createStandardXHR() { try { @@ -9,22 +26,10 @@ function createStandardXHR() { function createActiveXHR() { try { - return new window.ActiveXObject("Microsoft.XMLHTTP"); + return new window.ActiveXObject( "Microsoft.XMLHTTP" ); } catch( e ) {} } -var // Next active xhr id - xhrId = jQuery.now(), - - // active xhrs - xhrs = {}, - - // #5280: see below - xhrUnloadAbortInstalled, - - // XHR used to determine supports properties - testXHR; - // Create the request object // (This is still attached to ajaxSettings for backward compatibility) jQuery.ajaxSettings.xhr = window.ActiveXObject ? @@ -62,23 +67,6 @@ if ( jQuery.support.ajax ) { return { send: function( headers, complete ) { - // #5280: we need to abort on unload or IE will keep connections alive - if ( !xhrUnloadAbortInstalled ) { - - xhrUnloadAbortInstalled = 1; - - jQuery(window).bind( "unload", function() { - - // Abort all pending requests - jQuery.each( xhrs, function( _, xhr ) { - if ( xhr.onreadystatechange ) { - xhr.onreadystatechange( 1 ); - } - } ); - - } ); - } - // Get a new xhr var xhr = s.xhr(), handle, @@ -99,19 +87,24 @@ if ( jQuery.support.ajax ) { } } + // Override mime type if needed + if ( s.mimeType && xhr.overrideMimeType ) { + xhr.overrideMimeType( s.mimeType ); + } + // Requested-With header // Not set for crossDomain requests with no content // (see why at http://trac.dojotoolkit.org/ticket/9486) // Won't change header if already provided - if ( !( s.crossDomain && !s.hasContent ) && !headers["x-requested-with"] ) { - headers[ "x-requested-with" ] = "XMLHttpRequest"; + if ( !( s.crossDomain && !s.hasContent ) && !headers["X-Requested-With"] ) { + headers[ "X-Requested-With" ] = "XMLHttpRequest"; } // Need an extra try/catch for cross domain requests in Firefox 3 try { - jQuery.each( headers, function( key, value ) { - xhr.setRequestHeader( key, value ); - } ); + for ( i in headers ) { + xhr.setRequestHeader( i, headers[ i ] ); + } } catch( _ ) {} // Do send the request @@ -142,7 +135,7 @@ if ( jQuery.support.ajax ) { // Do not keep as active anymore if ( handle ) { xhr.onreadystatechange = jQuery.noop; - delete xhrs[ handle ]; + delete xhrCallbacks[ handle ]; } // If it's an abort @@ -152,7 +145,6 @@ if ( jQuery.support.ajax ) { xhr.abort(); } } else { - // Get info status = xhr.status; responseHeaders = xhr.getAllResponseHeaders(); responses = {}; @@ -175,33 +167,14 @@ if ( jQuery.support.ajax ) { // Filter status for non standard behaviors + // If the request is local and we have data: assume a success + // (success with no data won't get notified, that's the best we + // can do given current implementations) + if ( !status && s.isLocal && !s.crossDomain ) { + status = responses.text ? 200 : 404; // IE - #1450: sometimes returns 1223 when it should be 204 - if ( status === 1223 ) { + } else if ( status === 1223 ) { status = 204; - // Various - #8177: a Not Modified response was received - // yet no conditional request headers was provided - } else if ( status === 304 && - !headers[ "if-modified-since" ] && - !headers[ "if-none-match" ] ) { - status = 200; - // Status 0 encompasses several cases - } else if ( !status ) { - // Cross-domain - if ( s.crossDomain ) { - if ( !s.statusText ) { - // FF, Webkit (other?): There is no status text for errors - // 302 is the most generic cross-domain status code - // for errors, could be anything really (even a real 0) - status = 302; - } - // All same-domain: for local files, 0 is a success - } else if( s.isLocal ) { - status = 200; - // Opera: this notifies success for all requests - // (verified in 11.01). Patch welcome. - } - // Opera - #6060: sets status as 0 for 304 - // Patch welcome. } } } @@ -223,10 +196,15 @@ if ( jQuery.support.ajax ) { if ( !s.async || xhr.readyState === 4 ) { callback(); } else { - // Add to list of active xhrs + // Create the active xhrs callbacks list if needed + // and attach the unload handler + if ( !xhrCallbacks ) { + xhrCallbacks = {}; + xhrOnUnloadAbort(); + } + // Add to list of active xhrs callbacks handle = xhrId++; - xhrs[ handle ] = xhr; - xhr.onreadystatechange = callback; + xhr.onreadystatechange = xhrCallbacks[ handle ] = callback; } },