jQuery ajaxError Event

Beginner
5 min read
Updated: Nov 2026
3 examples
Ajax

Definition

ajaxError is a global AjaxEvent (since jQuery 1.0; .on() binding since 1.7). jQuery triggers it on document whenever a qualifying Ajax request fails. All registered handlers run.

Syntax

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

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

// Order (on failure):
// ajaxError  →  ajaxComplete  →  (ajaxStop when idle)

// Opt out for one call:
$.ajax({ url: "/x", global: false });

// Not fired for cross-domain script / JSONP requests
CallWhat it does
documentOnly valid bind target for global Ajax events
handlerfunction(event, jqXHR, ajaxSettings, thrownError)
ajaxSettings.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 .on("ajaxError", handler).
Step 2Make requests with $.ajax(), $.get(), $.post(), or .load().
Step 3Optionally filter with ajaxSettings.url (or other settings).

Return Value

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

jQuery Examples

Example 1 — Document Handler + Failing .load()

Official pattern: log a message when an Ajax request fails after a click.

jQuery
$(document).on("ajaxError", function () {
  $(".log").text("Triggered ajaxError handler.");
});

$(".trigger").on("click", function () {
  $(".result").load("ajax/missing.html");
});
Try It Yourself

How It Works

  1. Bind. .on("ajaxError") on document listens globally.
  2. Click. .load() starts an Ajax request under the hood.
  3. Fail. When the request errors, every ajaxError handler runs.

Example 2 — Filter by settings.url

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

jQuery
$(document).on("ajaxError", function (event, jqxhr, settings, thrownError) {
  if (settings.url === "ajax/missing.html") {
    $("div.log").text("Triggered ajaxError handler.");
  }
});
Try It Yourself

How It Works

  1. Args. jQuery passes event, jqxhr, settings, and thrownError.
  2. Compare. Check settings.url against the endpoint you care about.
  3. Act. Update UI only for matching failures.

Example 3 — Append “Error requesting page”

Official demo: append a short error string with the failed URL.

jQuery
$(document).on("ajaxError", function (event, request, settings) {
  $("#msg").append(" Error requesting page " + settings.url + " ");
});
Try It Yourself

How It Works

  1. Global. One handler covers every qualifying failed Ajax call.
  2. Append. Each failure adds another status fragment with the URL.
  3. Count. Parallel failures each fire their own ajaxError.

Frequently Asked Questions

ajaxError is a global AjaxEvent that fires whenever a qualifying jQuery Ajax request completes with an error — HTTP failures, transport errors, or parser errors. Bind it with $(document).on("ajaxError", handler) (since 1.7). Every registered handler runs for every failed request.
Since jQuery 1.9, all global Ajax event handlers must be bound to document — not window or other elements. Use $(document).on("ajaxError", fn) and remove with $(document).off("ajaxError", fn).
No. ajaxError runs only when a request fails. Successful responses trigger ajaxSuccess instead. Both are followed by ajaxComplete. Use ajaxError for global error UI; use ajaxSuccess when you only care about success.
ajaxError is a global document event — one handler observes every qualifying failed request on the page. fail() is per-request on the jqXHR from $.ajax() — it runs only for that call. Use global events for site-wide error UI; use fail() for request-specific handling.
When $.ajax() or $.ajaxSetup() sets global: false, that request opts out of all global Ajax events. ajaxError will not fire for it. Per-request fail() and always() still run.
The fourth handler argument, thrownError, receives the textual portion of the HTTP status when an HTTP error occurs — for example "Not Found" for 404 or "Internal Server Error" for 500. Pair it with jqXHR.status (the numeric code).
The shorthand method .ajaxError(handler) is deprecated as of jQuery 3.5. Prefer $(document).on("ajaxError", handler) — the event API this page teaches.

Next: .ajaxError() Method

See the deprecated shorthand and how to migrate to .on("ajaxError").

.ajaxError() Method 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.

9 people found this page helpful