From: jaubourg Date: Sun, 1 Apr 2012 23:54:19 +0000 (+0200) Subject: $.ajax now always returns an object implementing the Promise interface. Fixes #10944... X-Git-Tag: 1.8b1~231 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=395612bb152660226b93f7f096cd0146fc06991e;p=jquery.git $.ajax now always returns an object implementing the Promise interface. Fixes #10944. Unit tests amended. 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". --- diff --git a/src/ajax.js b/src/ajax.js index 2bcc1d0a2..ca9b63322 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -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; } diff --git a/test/unit/ajax.js b/test/unit/ajax.js index de680cb7d..431f14094 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -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" ); + }); });