jQuery .ajaxError() Method

Beginner
⚠️ Deprecated since 3.5
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Ajax events

What You’ll Learn

The .ajaxError() method is jQuery’s legacy shorthand for binding global ajaxError handlers on document. This tutorial covers $(document).ajaxError(handler), the handler signature (event, jqXHR, ajaxSettings, thrownError), why binding must target document since 1.9, migration to .on("ajaxError") and cleanup with .off("ajaxError"), and why the method is deprecated since jQuery 3.5. Unlike .click(), there is no trigger variant — only binding. The handler fires only on Ajax errors, not success. For the modern ajaxError event API, see the jQuery ajaxError event tutorial.

01

.ajaxError(fn)

Bind handler

02

document

Bind target

03

4 args

event, jqXHR, settings, thrownError

04

Errors only

Not on success

05

.on()

Modern bind

06

Deprecated

Since 3.5

Introduction

Before jQuery 1.7 unified events with .on() and .off(), global Ajax events had matching shorthand methods — .ajaxStart(), .ajaxSend(), .ajaxError(), and others. The .ajaxError(handler) method let you register a page-wide “request failed” callback in one short call: $(document).ajaxError(function(){ ... }). It has been available since jQuery 1.0 and remains in jQuery 3.x for backward compatibility but is marked deprecated since 3.5.

Understanding .ajaxError() matters when reading older admin panels, legacy jQuery plugins, and tutorials written before .on("ajaxError") became standard. Unlike DOM event shorthands such as .click(), .ajaxError() has only a binding form — there is no no-argument trigger variant. The handler runs only when a request fails — successful responses trigger ajaxSuccess instead. Modern jQuery binds the same global event explicitly: $(document).on("ajaxError", handler). This page documents the shorthand API and shows how to migrate.

Understanding the .ajaxError() Method

The official jQuery API describes .ajaxError() as a way to register a handler to be called when Ajax requests complete with an error. When you pass a function, jQuery registers it on document and calls it after every qualifying failed Ajax request — HTTP errors, transport failures, or parser errors. That is the same ajaxError global event that .on("ajaxError") handles today.

The handler receives four arguments even though the API signature lists only handler: event (the jQuery event object), jqXHR (the XMLHttpRequest wrapper — read jqXHR.status for the HTTP code), ajaxSettings (the settings object passed to $.ajax()), and thrownError (the textual portion of the HTTP status, such as “Not Found” for 404). Requests with global: false opt out — they do not trigger .ajaxError() handlers. Cross-domain script and cross-domain JSONP requests also do not trigger ajaxError.

⚠️
Deprecated since jQuery 3.5

Do not use $(document).ajaxError(handler) in new code. Replace it with $(document).on("ajaxError", handler). Remove handlers with $(document).off("ajaxError", handler) — not .unbind("ajaxError"). For event order, URL filtering, global: false, and comparison with .fail(), read the jQuery ajaxError event tutorial.

📝 Syntax

The .ajaxError() method has one signature from the official jQuery API — binding only. There is no trigger form unlike .click():

1. Bind a handler — .ajaxError( handler ) (deprecated since 3.5)

jQuery
$( document ).ajaxError( handler )
  • handler — function invoked when any qualifying Ajax request fails. Official docs list Function(); the callback actually receives function( event, jqXHR, ajaxSettings, thrownError ) { ... }.
  • Must attach to document since jQuery 1.9 — not window or other elements.
  • Fires only on error — not on successful requests.
  • Not called for cross-domain script or cross-domain JSONP requests.
  • Returns the jQuery object (document wrapped) for chaining.
  • Modern replacement: $( document ).on( "ajaxError", handler ).

2. Official jQuery API migration note

jQuery
// Deprecated binding

$( document ).ajaxError( function( event, jqXHR, settings, thrownError ) {

  console.log( "Request failed:", settings.url, jqXHR.status, thrownError );

} );



// Modern binding

$( document ).on( "ajaxError", function( event, jqXHR, settings, thrownError ) {

  console.log( "Request failed:", settings.url, jqXHR.status, thrownError );

} );



// Remove handler — use .off(), not .unbind()

$( document ).off( "ajaxError", myHandler );

3. Unbind — $(document).off("ajaxError" [, handler])

jQuery
$( document ).off( "ajaxError" );              // remove all ajaxError handlers

$( document ).off( "ajaxError", myHandler );   // remove one handler

Do not use deprecated .unbind("ajaxError") in new code. Store a named function reference when you need to remove a specific handler later.

Return value

  • .ajaxError(handler) returns the original jQuery object for chaining.
  • Handler return values are ignored by jQuery’s global Ajax event system.

⚡ Quick Reference

GoalLegacy (.ajaxError)Modern replacement
Bind global error handler$(document).ajaxError(fn)$(document).on("ajaxError", fn)
Handler signaturefunction(event, jqXHR, ajaxSettings, thrownError)
Read HTTP statusjqXHR.status + thrownError
Filter by URLif (ajaxSettings.url === "/api/x")
Opt out of global events$.ajax({ url: "/x", global: false })
Remove handlers$(document).off("ajaxError", fn)
Fires on success?No — only on error (ajaxSuccess handles success)
Cross-domain script / JSONPNot triggered
Trigger ajaxError programmaticallyNot supported — no no-arg .ajaxError() trigger form

📋 .ajaxError() vs .on("ajaxError") vs .off("ajaxError")

Three related APIs — know which one binds and which one removes handlers. Unlike .click(), there is no trigger shorthand.

.ajaxError()
deprecated

Legacy bind — $(document).ajaxError(fn) — use modern API for new code

.on("ajaxError")
bind

Modern binding since 1.7 — same behavior, clearer intent, reliable unbinding

.off("ajaxError")
remove

Unbind handlers — pair with .on("ajaxError") when cleaning up or in SPAs

ajaxError event
concept

Global AjaxEvent — fires only on failure; full guide on the event tutorial page

Examples Gallery

Five examples cover basic binding with a failing request, reading thrownError and jqXHR.status, filtering by settings.url, migration from .ajaxError() to .on("ajaxError"), and removing handlers with .off(). Use the Try-it links to run each snippet in the browser. Remember: binding with .ajaxError(handler) is deprecated — these demos show legacy syntax you will encounter in older code.

📚 Binding & Inspecting

Legacy .ajaxError() patterns for observing failed Ajax requests.

Example 1 — Basic $(document).ajaxError(fn) + Button Triggers Failing $.get() (404)

The canonical legacy pattern — attach a function on document that runs 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

$(document).ajaxError(fn) registers the function on document. jQuery internally routes this to .on("ajaxError", fn). The handler runs only when the request fails — a 200 response would trigger ajaxSuccess instead.

Example 2 — Read thrownError and jqXHR.status

The official handler signature passes four arguments — use the numeric status and status text for error reporting.

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

Although the API lists handler as Function(), jQuery passes event, jqXHR, ajaxSettings, and thrownError — same as the ajaxError event. Pair jqXHR.status with thrownError for complete error details.

Example 3 — Filter by settings.url (Legacy Syntax)

All ajaxError handlers run for every failed request — compare settings.url to narrow the callback.

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 );

  }

} );
Try It Yourself

How It Works

Because ajaxError fires for every global failure, URL filtering prevents unrelated endpoints from updating your UI. The settings object is the same ajaxSettings passed to $.ajax().

📈 Migration & Cleanup

Compare legacy and modern APIs, and remove handlers with .off().

Example 4 — Migration: .ajaxError(fn) vs .on("ajaxError", fn)

Both bind the same global event — .on() is the recommended API for new code.

jQuery
// Legacy — deprecated since jQuery 3.5

$( document ).ajaxError( function() {

  $( "#out" ).text( "Legacy: .ajaxError(function(){ ... })." );

} );



// Modern — use for new code

$( document ).on( "ajaxError", function() {

  $( "#out" ).text( "Modern: .on('ajaxError', function(){ ... })." );

} );
Try It Yourself

How It Works

Under the hood, .ajaxError(fn) calls .on("ajaxError", fn). Migration is a straight rename with no behavior change for simple bindings. The modern API also pairs cleanly with .off("ajaxError", fn) for teardown.

Example 5 — Remove Handler with .off("ajaxError")

Store a named function reference, 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 + "." );

} );
Try It Yourself

How It Works

Anonymous functions passed to .ajaxError() cannot be removed individually — use a named function. .off("ajaxError", onError) removes exactly that handler. Do not call deprecated .unbind("ajaxError") in new code.

🚀 Common Use Cases

  • Reading legacy code — recognize $(document).ajaxError(fn) as equivalent to .on("ajaxError", fn) in older admin panels.
  • Global error logging — legacy debug panels log failed requests via .ajaxError() — migrate to .on() when refactoring.
  • Status inspection — read jqXHR.status and thrownError in a global handler for development logging.
  • URL filtering — compare settings.url inside .ajaxError(fn) before updating the DOM.
  • SPA teardown — remove global handlers with .off("ajaxError", fn) when unmounting a view.
  • Third-party isolation — requests with global: false still opt out of .ajaxError() handlers.

🧠 How .ajaxError() Routes Through jQuery

1

You call .ajaxError(handler)

jQuery treats the function argument as a bind request on document. There is no no-argument trigger form — unlike .click().

bind only
2

Internal delegation

.ajaxError(handler) delegates internally to .on("ajaxError", handler) — the handler is stored in jQuery’s event system on document.

.on("ajaxError")
3

Ajax request fails

On HTTP error, transport failure, or parser error, jQuery triggers ajaxError on document — unless the request used global: false or is cross-domain script/JSONP.

errors only
4

Handler runs with four arguments

function(event, jqXHR, ajaxSettings, thrownError) — read jqXHR.status, filter by ajaxSettings.url, and log thrownError. Returns the jQuery object for chaining.

📝 Notes

  • Deprecated since jQuery 3.5 — do not use $(document).ajaxError(handler) in new code. Use .on("ajaxError") instead. Available since jQuery 1.0.
  • Attach to document since jQuery 1.9 — not window or other elements.
  • Binding only — no no-argument trigger form unlike .click() or .mousedown().
  • Handler receives (event, jqXHR, ajaxSettings, thrownError) even though the API lists handler as Function().
  • Fires only on error — successful requests trigger ajaxSuccess, not ajaxError.
  • Not called for cross-domain script or cross-domain JSONP requests.
  • Remove handlers with $(document).off("ajaxError", handler) — not .unbind("ajaxError").
  • Requests with global: false do not trigger .ajaxError() handlers.
  • Legacy code using .ajaxError() still runs in jQuery 3.x — migrate when you refactor those files.
  • For event order, comparison with .fail(), and the complete modern workflow, see the jQuery ajaxError event tutorial.

Browser Support

The underlying ajaxError global event works wherever jQuery’s Ajax transport runs. The .ajaxError() method itself is a jQuery API — it works in jQuery 1.x, 2.x, and 3.x when bound to document since 1.9. Binding via .ajaxError(handler) is deprecated since 3.5 but still functional.

jQuery 1.0+

jQuery .ajaxError() method

Supported across all jQuery versions you are likely to maintain. Deprecated binding since 3.5 — use .on('ajaxError') for new projects. Requires jQuery Ajax — fetch() and axios do not trigger jQuery global events.

100% With jQuery Ajax
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All 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.

Conclusion

The jQuery .ajaxError() method is the legacy shorthand for binding global Ajax error handlers on document. Use $(document).ajaxError(handler) to register a callback — deprecated since jQuery 3.5, available since 1.0. The handler receives (event, jqXHR, ajaxSettings, thrownError) and runs only when a qualifying request fails.

For new code, prefer $(document).on("ajaxError", fn) for binding and $(document).off("ajaxError", fn) for cleanup. When you encounter .ajaxError() in older projects, recognize it, understand it, and migrate when practical. For the full modern ajaxError workflow — event order, URL filtering, and global: false — continue with the jQuery ajaxError event tutorial, then learn ajaxSuccess for the global success hook, then ajaxComplete for the always-finish hook.

💡 Best Practices

✅ Do

  • Use $(document).on("ajaxError", handler) for all new global bindings
  • Bind on document — required since jQuery 1.9
  • Store named function references when you need to remove handlers later
  • Remove handlers with $(document).off("ajaxError", fn) on SPA teardown
  • Read the modern ajaxError event tutorial for full API coverage

❌ Don’t

  • Write new code with deprecated $(document).ajaxError(handler)
  • Attach global handlers to window — use document
  • Expect a no-arg .ajaxError() trigger form — it does not exist
  • Use .unbind("ajaxError") — it is also deprecated; use .off()
  • Assume ajaxError fires on success — it runs only on failure

Key Takeaways

Knowledge Unlocked

Six things to remember about .ajaxError()

Legacy bind, modern migrate.

6
Core concepts
doc 02

document

Bind here

Since 1.9
03

4 args

event, jqXHR, settings, thrownError

Handler
.on 04

.on("ajaxError")

Modern

Replace
.off 05

.off()

Remove

Cleanup
3.5 06

Deprecated

Since 3.5

Migrate

❓ Frequently Asked Questions

The .ajaxError(handler) method binds a global function that runs whenever any qualifying jQuery Ajax request completes with an error — HTTP failures, transport errors, or parser errors. You must attach it to document: $(document).ajaxError(fn). Unlike .click(), there is no no-argument trigger form — only binding. The method has been deprecated since jQuery 3.5; use $(document).on('ajaxError', handler) instead.
Yes — $(document).ajaxError(handler) is deprecated since jQuery 3.5 (available since jQuery 1.0). Use $(document).on('ajaxError', handler) for new code. Existing legacy code continues to run in jQuery 3.x; migrate when you touch those files. Remove handlers with $(document).off('ajaxError', handler), not the deprecated .unbind() API.
ajaxError is a global Ajax event, not a DOM event on a specific element. Since jQuery 1.9, all global Ajax handlers — including .ajaxError(handler) — must be bound to document, not window or other elements. The correct legacy pattern is $(document).ajaxError(fn); the modern equivalent is $(document).on('ajaxError', fn).
The official jQuery API lists handler as Type Function(), but the callback actually receives four arguments — the same as the ajaxError event: function(event, jqXHR, ajaxSettings, thrownError). Use jqXHR.status for the numeric HTTP code and thrownError for the status text (e.g. "Not Found" for 404).
Replace $(document).ajaxError(fn) with $(document).on('ajaxError', fn). Replace cleanup via .unbind('ajaxError') with $(document).off('ajaxError', fn). Under the hood, .ajaxError(handler) already delegates to .on('ajaxError') — migration is a straight rename with no behavior change for simple bindings.
Yes. jQuery 3.x keeps .ajaxError(handler) for backward compatibility with jQuery 1.x and 2.x codebases. It still binds correctly on document. The API docs mark it deprecated since 3.5 to steer new projects toward .on(). Do not use .ajaxError(handler) in new code — prefer the modern API covered in the jQuery ajaxError event tutorial.
Did you know?

Unlike DOM event shorthands such as .click(), which support both binding (.click(fn)) and triggering (no-arg .click()), .ajaxError() is bind-only. You cannot programmatically fire global Ajax events through a no-argument .ajaxError() call — they fire automatically when jQuery’s Ajax transport encounters an error. The deprecated method is simply sugar for .on("ajaxError", fn) on document. Cross-domain script and JSONP requests never trigger it.

Next: ajaxSuccess Event

Learn the global handler that runs when Ajax requests complete successfully.

ajaxSuccess event tutorial →

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.

6 people found this page helpful