.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 });
Call
What it does
document
Only valid bind target for global Ajax events
handler
function(event, jqXHR, settings, thrownError)
settings.url
Filter which failed requests you react to
global: false
Skip 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
Parameter
Description
Step 1
Attach the handler to document with .ajaxError(handler) (legacy) or .on("ajaxError", handler) (preferred).
Step 2
Trigger a failing request with $.ajax(), $.get(), $.post(), or .load().
Step 3
Read jqXHR.status and thrownError; optionally filter with settings.url.
Return Value
Return Value
Description
jQuery object
Most 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");
});
Matching URL fails → log shows URL + status
Other URLs fail → handler runs, if-block skipped
How It Works
Global. Every failed qualifying request invokes the handler.
Compare. Check settings.url against the endpoint you care about.
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.