How can I force a successful response from $.ajax
to fire its "fail" handlers from within a "done" handler?
The jQuery documentation says the jqXHR
object implements the Promise interface, which means it has no reject
method.
Below shows some code listening for failures on a jqXHR
object created inside another scope.
If the successful response is actually a well-formed error, I want to trigger handlers that are listening for "fail".
// common function
function doAjax(options) {
return $.ajax(options).fail(function() {
// common error handling
}.done(function(result, status, xhr) {
if (result.isBAD) {
xhr.reject(); // <- not a jqXHR method
}
}));
}
// listening elsewhere
doAjax(options).fail(function() {
// specific error handling
});