]> source.dussan.org Git - jquery.git/commitdiff
Reworks how values of parameters passed to error callbacks are determined. Fixes...
authorjaubourg <j@ubourg.net>
Tue, 25 Jan 2011 15:08:19 +0000 (16:08 +0100)
committerjaubourg <j@ubourg.net>
Tue, 25 Jan 2011 15:08:19 +0000 (16:08 +0100)
src/ajax.js
src/ajax/xhr.js
test/unit/ajax.js

index 89d60e1d008e75788d6e8de5bad8bffb229c36f7..8f8bc60ab07fc1105ff4a1377857fdb8dc238854 100644 (file)
@@ -400,8 +400,9 @@ jQuery.extend({
 
                                // Cancel the request
                                abort: function( statusText ) {
+                                       statusText = statusText || "abort";
                                        if ( transport ) {
-                                               transport.abort( statusText || "abort" );
+                                               transport.abort( statusText );
                                        }
                                        done( 0, statusText );
                                        return this;
@@ -438,7 +439,7 @@ jQuery.extend({
 
                        var isSuccess,
                                success,
-                               error = ( statusText = statusText || "error" ),
+                               error,
                                response = responses ? ajaxHandleResponses( s, jXHR, responses ) : undefined,
                                lastModified,
                                etag;
@@ -476,6 +477,16 @@ jQuery.extend({
                                                error = "" + e;
                                        }
                                }
+                       } else {
+                               // We extract error from statusText
+                               // then normalize statusText and status for non-aborts
+                               error = statusText;
+                               if( status ) {
+                                       statusText = "error";
+                                       if ( status < 0 ) {
+                                               status = 0;
+                                       }
+                               }
                        }
 
                        // Set data for the fake xhr object
@@ -634,7 +645,7 @@ jQuery.extend({
 
                        // If no transport, we auto-abort
                        if ( !transport ) {
-                               done( 0, "notransport" );
+                               done( -1, "No Transport" );
                        } else {
                                // Set state as sending
                                state = jXHR.readyState = 1;
@@ -653,9 +664,8 @@ jQuery.extend({
                                        transport.send( requestHeaders, done );
                                } catch (e) {
                                        // Propagate exception as error if not done
-                                       if ( status === 1 ) {
-                                               done( 0, "error", "" + e );
-                                               jXHR = false;
+                                       if ( status < 2 ) {
+                                               done( -1, "" + e );
                                        // Simply rethrow otherwise
                                        } else {
                                                jQuery.error( e );
index 5629dcd60b8fd671dce1fd5e9fbcd2b2960d3c42..b820642392b5f6942e02f22d8b5a14a96c51b086 100644 (file)
@@ -108,12 +108,9 @@ if ( jQuery.support.ajax ) {
                                        } catch( _ ) {}
 
                                        // Do send the request
-                                       try {
-                                               xhr.send( ( s.hasContent && s.data ) || null );
-                                       } catch( e ) {
-                                               complete( 0, "error", "" + e );
-                                               return;
-                                       }
+                                       // This may raise an exception which is actually
+                                       // handled in jQuery.ajax (so no try/catch here)
+                                       xhr.send( ( s.hasContent && s.data ) || null );
 
                                        // Listener
                                        callback = function( _, isAbort ) {
index b44f0773f2f40303ff7545a9a78ebe2c9fe9c65c..d01837239b01455de6bfe0226444c165ac2fb081 100644 (file)
@@ -240,6 +240,47 @@ test("jQuery.ajax() - error callbacks", function() {
        });
 });
 
+test("jQuery.ajax() - textStatus and errorThrown values", function() {
+
+       var nb = 3;
+
+       expect( 2 * nb );
+       stop();
+
+       function startN() {
+               if ( !( --nb ) ) {
+                       start();
+               }
+       }
+
+       jQuery.ajax({
+               url: url("data/nonExistingURL"),
+               error: function( _ , textStatus , errorThrown ){
+                       strictEqual( textStatus, "error", "textStatus is 'error' for 404" );
+                       strictEqual( errorThrown, "Not Found", "errorThrown is 'Not Found' for 404");
+                       startN();
+               }
+       });
+
+       jQuery.ajax({
+               url: url("data/name.php?wait=5"),
+               error: function( _ , textStatus , errorThrown ){
+                       strictEqual( textStatus, "abort", "textStatus is 'abort' for abort" );
+                       strictEqual( errorThrown, "abort", "errorThrown is 'abort' for abort");
+                       startN();
+               }
+       }).abort();
+
+       jQuery.ajax({
+               url: url("data/name.php?wait=5"),
+               error: function( _ , textStatus , errorThrown ){
+                       strictEqual( textStatus, "mystatus", "textStatus is 'mystatus' for abort('mystatus')" );
+                       strictEqual( errorThrown, "mystatus", "errorThrown is 'mystatus' for abort('mystatus')");
+                       startN();
+               }
+       }).abort( "mystatus" );
+});
+
 test("jQuery.ajax() - responseText on error", function() {
 
        expect( 1 );