I think that you want to modify the ajax deferred itself to trigger different callbacks based upon the return state. You can't because Promise
objects can be resolved only once. Look at this example:
var fai = $.Deferred();
fai.then(function () {
fai.reject();
console.log(fai.state());
}).done(function () {
console.log(fai.state());
});
fai.resove();
http://jsfiddle.net/ExplosionPIlls/QreYY/
The chain of events would be calling .resolve
, and then the .then
callback. .then
calls .reject
. However, if you run this you will see that the state after the .reject
call is still "resolved," and the .done
callback gets triggered.
That is to say if your ajax request succeeds it will already be set to the .resolved
state and you can't undo that.
Instead, you can do something like this, which I think is simpler anyway:
$.ajax(...).done(function (json) {
if (json && json.status == 'ok') {
//handle success right here
}
else {
//handle failure right here
}
});