.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.
Remember
// 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 });
Recognize the legacy bind, know the handler args, then migrate to .on().
Attach the handler to document with .ajaxError(handler) (legacy) or .on("ajaxError", handler) (preferred).
Trigger a failing request with $.ajax(), $.get(), $.post(), or .load().
Read jqXHR.status and thrownError; optionally filter with settings.url.
Remove handlers with $(document).off("ajaxError", handler) — not .unbind().
Use global: false on requests that must stay silent.
Piece
Required?
Role
document
Yes (1.9+)
Only valid bind target for global Ajax events
handler
Yes
function(event, jqXHR, settings, thrownError)
settings.url
Optional
Filter which failed requests you react to
global: false
Optional
Skip all global Ajax events for that call
Pseudocode
Pseudocode
bind ajaxError on document with handler(event, jqXHR, settings, thrownError)
// legacy: $(document).ajaxError(handler)
// modern: $(document).on("ajaxError", handler)
when a jQuery Ajax request finishes with an error:
if request.global === false:
do not fire ajaxError
return
// note: cross-domain script / JSONP do not fire ajaxError
fire ajaxError
for each registered handler:
call handler(event, jqXHR, settings, thrownError)
fire ajaxComplete next
if no other global Ajax requests are active:
fire ajaxStop
Reference
Cheat Sheet
Keep these facts handy while reading or migrating legacy error handlers.
Topic
Detail
Legacy bind
$(document).ajaxError(handler) ⚠ deprecated 3.5
Modern bind
$(document).on("ajaxError", handler)
Unbind
$(document).off("ajaxError" [, handler])
Signature
function(event, jqXHR, settings, thrownError)
When
Error only — not on success
Since
Method 1.0 · document-only 1.9+ · deprecated 3.5
Status / text
jqXHR.status · thrownError
Opt out
global: false in $.ajax() / $.ajaxSetup()
Trigger form
None — bind only (unlike .click())
Try it
Live Preview
Run a failing $.get() and watch the legacy .ajaxError() handler update the log.
Live demo uses a public API. Needs network access. Binding uses deprecated .ajaxError() for teaching.
Result
Ready — pick an action and click Run.
Code
jQuery Examples
Short, copy-ready snippets. These show legacy .ajaxError() you will see in older code — prefer .on("ajaxError") for new work. Use Try It Yourself for a live editor.
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");
});
Both handlers fire when a request fails
Legacy message from .ajaxError(fn)
Modern message from .on("ajaxError", fn)
Behavior matches — only the API name differs
How It Works
Same event..ajaxError(fn) delegates to .on("ajaxError", fn).
Rename. Migration is usually a one-line API change.
Cleanup. Pair modern binds with .off("ajaxError", fn).
New code. Always prefer .on() / .off().
Example 5 — Remove Handler with .off("ajaxError")
Store a named function, then remove it with .off() — not deprecated .unbind().
jQuery
var count = 0;
function onError() {
count++;
$(".log").text("Handler active — ajaxError count: " + count);
}
$(document).ajaxError(onError);
$("#remove").on("click", function () {
$(document).off("ajaxError", onError);
$(".log").text("Handler removed — count stays " + count + ".");
});
Failing $.get() → count increments
Click Remove → .off("ajaxError", onError)
Next 404 still fails — handler no longer runs
How It Works
Named fn. Anonymous handlers cannot be removed individually.
Bind..ajaxError(onError) registers that reference.
Remove..off("ajaxError", onError) drops exactly that handler.
Avoid. Do not use deprecated .unbind("ajaxError") in new code.
Edge Cases & Pitfalls
Check these before relying on global Ajax error handlers.
deprecated
Shorthand is deprecated since 3.5
New code should use $(document).on("ajaxError", handler). The shorthand still works for legacy apps.
target
Bind on document only (1.9+)
Handlers on window or other elements will not receive global Ajax events.
scope
Error only — not success
Successful requests fire ajaxSuccess, not ajaxError. Use ajaxComplete when you need both.
jsonp
Cross-domain script / JSONP skip it
Per the jQuery API, ajaxError is not called for cross-domain script and cross-domain JSONP requests.
global
global: false stays silent
Those requests never trigger .ajaxError(). Per-request .fail() still works.
Compatibility
Browser Support
The .ajaxError() method has been part of jQuery since 1.0. Since 1.9, handlers must attach to document. Binding via .ajaxError(handler) is deprecated since 3.5 but still functional — prefer .on("ajaxError").
✓ Deprecated · jQuery 1.0+
jQuery .ajaxError()
Works wherever your loaded jQuery version runs. Prefer a current jQuery 3.x build and migrate bindings to .on('ajaxError'). Requires jQuery Ajax — fetch() and axios do not trigger jQuery global events.
100%With jQuery Ajax
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.ajaxError()Legacy API
Bottom line: Safe in legacy jQuery projects. Migrate bindings to .on('ajaxError') and cleanup to .off('ajaxError') when updating code.
Analysis
Performance Notes
Approach
Cost
Best for
One filtered global error handler
Cheap
Site-wide error toasts / logging
Per-request .fail()
Cheap
Cleanup for a single call
Heavy DOM work on every error
Expensive
Avoid — filter by URL first
Global error handlers are fine when they stay light. Filter by URL before touching the DOM, and prefer .fail() for request-specific work.
Remember
Key Takeaways
Role: legacy global “request failed” bind — error only.
Bind:$(document).ajaxError(fn) on document only (1.9+) — deprecated since 3.5.
Args:(event, jqXHR, settings, thrownError) — use status + URL filter.
Migrate:.on("ajaxError") / .off("ajaxError") for new code.
One line:.ajaxError() is the deprecated shorthand for binding global Ajax error handlers on document — prefer .on("ajaxError").
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.