Fixes #4964. Adds a statusCode object together with a new statusCode method on the jXHR object (deferred behaviour). They accept a map of statusCode/callback(s). Callbacks are fired when the status code of the response correponds to the key (as a success or an error callback depending on how the request completed). Unit tests added.
This commit is contained in:
parent
57956152d8
commit
44fc87f66c
2 changed files with 97 additions and 0 deletions
27
src/ajax.js
27
src/ajax.js
|
@ -265,6 +265,8 @@ jQuery.extend({
|
|||
// Deferreds
|
||||
deferred = jQuery.Deferred(),
|
||||
completeDeferred = jQuery._Deferred(),
|
||||
// Status-dependent callbacks
|
||||
statusCode = s.statusCode || {},
|
||||
// Headers (they are sent all at once)
|
||||
requestHeaders = {},
|
||||
// Response headers
|
||||
|
@ -520,6 +522,9 @@ jQuery.extend({
|
|||
deferred.fireReject( callbackContext , [ jXHR , statusText , error ] );
|
||||
}
|
||||
|
||||
// Status-dependent callbacks
|
||||
jXHR.statusCode( statusCode );
|
||||
|
||||
if ( s.global ) {
|
||||
globalEventContext.trigger( "ajax" + ( isSuccess ? "Success" : "Error" ) ,
|
||||
[ jXHR , s , isSuccess ? success : error ] );
|
||||
|
@ -543,6 +548,28 @@ jQuery.extend({
|
|||
jXHR.error = jXHR.fail;
|
||||
jXHR.complete = completeDeferred.done;
|
||||
|
||||
// Status-dependent callbacks
|
||||
jXHR.statusCode = function( map ) {
|
||||
if ( map ) {
|
||||
var resolved = jXHR.isResolved(),
|
||||
tmp;
|
||||
if ( resolved || jXHR.isRejected() ) {
|
||||
tmp = map[ jXHR.status ];
|
||||
if ( tmp ) {
|
||||
if ( map === statusCode ) {
|
||||
delete statusCode[ jXHR.status ];
|
||||
}
|
||||
jXHR[ resolved ? "done" : "fail" ]( tmp );
|
||||
}
|
||||
} else {
|
||||
for( tmp in map ) {
|
||||
statusCode[ tmp ] = [ statusCode[ tmp ] , map[ tmp ] ];
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
// Remove hash character (#7531: and string promotion)
|
||||
s.url = ( "" + s.url ).replace( rhash , "" );
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue