Permalink
Browse files

$.ajax now always returns an object implementing the Promise interfac…

…e. 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".
  • Loading branch information...
1 parent a29d482 commit 395612bb152660226b93f7f096cd0146fc06991e @jaubourg jaubourg committed Apr 1, 2012
Showing with 15 additions and 10 deletions.
  1. +3 −3 src/ajax.js
  2. +12 −7 test/unit/ajax.js
View
@@ -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;
}
View
@@ -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" );
+ });
});

0 comments on commit 395612b

Please sign in to comment.