jQuery .ajaxError() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Ajax

Definition

.ajaxError(handler) is jQuery’s legacy shorthand (since 1.0) for binding a global handler that runs when any qualifying Ajax request fails. Attach it to document. Unlike .click(), there is no no-argument trigger form — binding only.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
// Deprecated shorthand
$(document).ajaxError(function (event, jqXHR, settings, thrownError) {
  // runs only when a qualifying Ajax request fails
});

// Modern replacement
$(document).on("ajaxError", handler);

// Opt out for one call:
$.ajax({ url: "/x", global: false });
CallWhat it does
documentOnly valid bind target for global Ajax events
handlerfunction(event, jqXHR, settings, thrownError)
settings.urlFilter which failed requests you react to
global: falseSkip all global Ajax events for that call

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Attach the handler to document with .ajaxError(handler) (legacy) or .on("ajaxError", handler) (preferred).
Step 2Trigger a failing request with $.ajax(), $.get(), $.post(), or .load().
Step 3Read jqXHR.status and thrownError; optionally filter with settings.url.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — Basic $(document).ajaxError(fn)

Canonical legacy pattern: log when any qualifying Ajax request fails.

jQuery
$(document).ajaxError(function () {
  $(".log").text("Handler ran via .ajaxError(function(){ ... }).");
});

$("#fetch").on("click", function () {
  $.get("https://httpbin.org/status/404");
});
Try It Yourself

How It Works

  1. Bind. .ajaxError(fn) registers the function on document.
  2. Click. $.get() starts a request that returns 404.
  3. Fail. jQuery triggers ajaxError — not ajaxSuccess.

Example 2 — Read jqXHR.status and thrownError

The handler receives four arguments — use them for status code and status text.

jQuery
$(document).ajaxError(function (event, jqXHR, settings, thrownError) {
  $(".log").text(
    "Status: " + jqXHR.status + " | thrownError: " + thrownError
  );
});

$("#fetch").on("click", function () {
  $.get("https://httpbin.org/status/404");
});
Try It Yourself

How It Works

  1. Args. jQuery passes event, jqXHR, settings, and thrownError.
  2. Code. jqXHR.status is the numeric HTTP status (404 here).
  3. Text. thrownError holds status text such as "Not Found".

Example 3 — Filter by settings.url

Handlers run for every failed request — narrow them with the settings object.

jQuery
var targetUrl = "https://httpbin.org/status/404";

$(document).ajaxError(function (event, jqXHR, settings, thrownError) {
  if (settings.url === targetUrl) {
    $(".log").text(
      "Filtered match — URL: " + settings.url +
      " | status: " + jqXHR.status
    );
  }
});
Try It Yourself

How It Works

  1. Global. Every failed qualifying request invokes the handler.
  2. Compare. Check settings.url against the endpoint you care about.
  3. React. Update the UI only for the watched URL.

Frequently Asked Questions

The .ajaxError(handler) method binds a global function that runs whenever any qualifying jQuery Ajax request completes with an error. Attach it to document: $(document).ajaxError(fn). It is deprecated since jQuery 3.5 — use $(document).on("ajaxError", handler) instead.
Yes. $(document).ajaxError(handler) is deprecated since jQuery 3.5 (available since 1.0). Prefer $(document).on("ajaxError", handler) for new code. Legacy bindings still work in jQuery 3.x; migrate when you touch those files. Remove handlers with $(document).off("ajaxError", handler).
ajaxError is a global Ajax event, not a DOM event on a specific element. Since jQuery 1.9, all global Ajax handlers must be bound to document. Use $(document).ajaxError(fn) (legacy) or $(document).on("ajaxError", fn) (modern).
Four: function(event, jqXHR, ajaxSettings, thrownError). Use jqXHR.status for the HTTP code and thrownError for status text (for example "Not Found" on 404). Failed requests trigger ajaxError; successful ones trigger ajaxSuccess instead.
Replace $(document).ajaxError(fn) with $(document).on("ajaxError", fn). Replace .unbind("ajaxError") with $(document).off("ajaxError", fn). Under the hood, .ajaxError(handler) already delegates to .on("ajaxError") — migration is a straight rename for simple bindings.
No. It runs only when a qualifying request fails (HTTP error, transport error, or parser error). Use ajaxSuccess for success-only, or ajaxComplete for both outcomes.

Next: ajaxSuccess Event

Learn the global handler that runs when a jQuery Ajax request succeeds.

ajaxSuccess Event tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful