deferred.fail()

Add handlers to be called when the Deferred object is rejected.

.deferred.fail(failCallbacks, failCallbacks)🡢 Deferred

failCallbacks FunctionA function, or array of functions, that are called when the Deferred is rejected.
failCallbacks FunctionOptional additional functions, or arrays of functions, that are called when the Deferred is rejected.

The deferred.fail() method accepts one or more arguments, all of which can be either a single function or an array of functions. When the Deferred is rejected, the failCallbacks are called. Callbacks are executed in the order they were added. Since deferred.fail() returns the deferred object, other methods of the deferred object can be chained to this one, including additional deferred.fail() methods. The failCallbacks are executed using the arguments provided to the deferred.reject() or deferred.rejectWith() method call in the order they were added. For more information, see the documentation for Deferred object.

Since the jQuery.get method returns a jqXHR object, which is derived from a Deferred, you can attach a success and failure callback using the deferred.done() and deferred.fail() methods.

HTML
$.get("test.php")
  .done(function () {
    alert("$.get succeeded");
  })
  .fail(function () {
    alert("$.get failed!");
  });
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)