Simplifies status normalization in xhr transport. Local file test modified for clarity.
[jquery.git] / src / ajax / xhr.js
index 97db079..c48ac90 100644 (file)
@@ -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 {
@@ -13,22 +30,6 @@ function createActiveXHR() {
        } catch( e ) {}
 }
 
-var // Next active xhr id
-       xhrId = jQuery.now(),
-
-       // active xhrs
-       xhrs = {},
-
-       // #5280: see below
-       xhrUnloadAbortInstalled,
-
-       // XHR used to determine supports properties
-       testXHR,
-
-       // Keep track of isLocal in case it gets removed
-       // from ajaxSettings later on
-       protocolIsLocal = jQuery.ajaxSettings.isLocal;
-
 // Create the request object
 // (This is still attached to ajaxSettings for backward compatibility)
 jQuery.ajaxSettings.xhr = window.ActiveXObject ?
@@ -38,12 +39,9 @@ jQuery.ajaxSettings.xhr = window.ActiveXObject ?
         * Additionally XMLHttpRequest can be disabled in IE7/IE8 so
         * we need a fallback.
         */
-       ( protocolIsLocal ?
-               createActiveXHR :
-               function() {
-                       return createStandardXHR() || createActiveXHR();
-               }
-       ) :
+       function() {
+               return !this.isLocal && createStandardXHR() || createActiveXHR();
+       } :
        // For all other browsers, use the standard XMLHttpRequest object
        createStandardXHR;
 
@@ -69,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,
@@ -106,6 +87,11 @@ 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)
@@ -116,9 +102,9 @@ if ( jQuery.support.ajax ) {
 
                                        // 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
@@ -149,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
@@ -159,7 +145,6 @@ if ( jQuery.support.ajax ) {
                                                                                xhr.abort();
                                                                        }
                                                                } else {
-                                                                       // Get info
                                                                        status = xhr.status;
                                                                        responseHeaders = xhr.getAllResponseHeaders();
                                                                        responses = {};
@@ -181,29 +166,14 @@ if ( jQuery.support.ajax ) {
                                                                        }
 
                                                                        // Filter status for non standard behaviors
-
-                                                                       // IE - #1450: sometimes returns 1223 when it should be 204
-                                                                       if ( status === 1223 ) {
-                                                                               status = 204;
-                                                                       // 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( protocolIsLocal ) {
-                                                                                       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.
-                                                                       }
+                                                                       status =
+                                                                               // 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)
+                                                                               !status && s.isLocal ?
+                                                                               ( responses.text ? 200 : 404 ) :
+                                                                               // IE - #1450: sometimes returns 1223 when it should be 204
+                                                                               ( status === 1223 ? 204 : status );
                                                                }
                                                        }
                                                } catch( firefoxAccessException ) {
@@ -224,10 +194,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;
                                        }
                                },