]> source.dussan.org Git - jquery.git/commitdiff
$.ajax now always returns an object implementing the Promise interface. Fixes #10944...
authorjaubourg <j@ubourg.net>
Sun, 1 Apr 2012 23:54:19 +0000 (01:54 +0200)
committerjaubourg <j@ubourg.net>
Sun, 1 Apr 2012 23:54:19 +0000 (01:54 +0200)
For back-compat, in case of an early abort, callbacks passed in the options are not called (while subsequent callbacks attached to the returned Promise are).
For early abort triggered by returning false in beforeSend, statusText is "canceled".

src/ajax.js
test/unit/ajax.js

index 2bcc1d0a2cb558b5fe34ce733b8313baad440440..ca9b63322bcb0c66cf764aea91fd4b19850ab9a9 100644 (file)
@@ -643,7 +643,7 @@ jQuery.extend({
 
                // If request was aborted inside a prefilter, stop there
                if ( state === 2 ) {
-                       return false;
+                       return jqXHR;
                }
 
                // We can fire global events as of now if asked to
@@ -717,8 +717,8 @@ jQuery.extend({
                // Allow custom headers/mimetypes and early abort
                if ( s.beforeSend && ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || state === 2 ) ) {
                                // Abort if not done already
-                               jqXHR.abort();
-                               return false;
+                               done( 0, "canceled" );
+                               return jqXHR;
 
                }
 
index de680cb7d0b102eb4017d770f6f6b4e383714592..431f14094d1570b2c241b8fb0fa197c8368c5d2f 100644 (file)
@@ -857,7 +857,7 @@ test("jQuery.ajax - beforeSend", function() {
 
 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
        expect(2);
-       var request = jQuery.ajax({
+       jQuery.ajax({
                url: url("data/name.html"),
                beforeSend: function() {
                        ok( true, "beforeSend got called, canceling" );
@@ -872,13 +872,14 @@ test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
                error: function() {
                        ok( false, "request didn't get canceled" );
                }
+       }).fail(function( _, reason ) {
+               strictEqual( reason, "canceled", "canceled request must fail with 'canceled' status text" );
        });
-       ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
 });
 
 test("jQuery.ajax - beforeSend, cancel request manually", function() {
        expect(2);
-       var request = jQuery.ajax({
+       jQuery.ajax({
                url: url("data/name.html"),
                beforeSend: function(xhr) {
                        ok( true, "beforeSend got called, canceling" );
@@ -893,8 +894,9 @@ test("jQuery.ajax - beforeSend, cancel request manually", function() {
                error: function() {
                        ok( false, "request didn't get canceled" );
                }
+       }).fail(function( _, reason ) {
+               strictEqual( reason, "abort", "manually canceled request must fail with 'abort' status text" );
        });
-       ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
 });
 
 window.foobar = null;
@@ -2109,13 +2111,14 @@ test( "jQuery.ajax - Context with circular references (#9887)", 2, function () {
                context = {};
        context.field = context;
        try {
-               success = !jQuery.ajax( "non-existing", {
+               jQuery.ajax( "non-existing", {
                        context: context,
                        beforeSend: function() {
                                ok( this === context, "context was not deep extended" );
                                return false;
                        }
                });
+               success = true;
        } catch (e) { console.log( e ); }
        ok( success, "context with circular reference did not generate an exception" );
 });
@@ -2315,12 +2318,14 @@ test("jQuery.ajax - abort in prefilter", function() {
                }
        });
 
-       strictEqual( jQuery.ajax({
+       jQuery.ajax({
                abortInPrefilter: true,
                error: function() {
                        ok( false, "error callback called" );
                }
-       }), false, "Request was properly aborted early by the prefilter" );
+       }).fail(function( _, reason ) {
+               strictEqual( reason, 'abort', "Request aborted by the prefilter must fail with 'abort' status text" );
+       });
 
 });