jQuery ajaxError Event

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Ajax events

What You’ll Learn

The ajaxError event fires when a qualifying jQuery Ajax request completes with an error — not on success. This tutorial covers binding on document with .on("ajaxError"), the handler signature (event, jqXHR, ajaxSettings, thrownError), filtering by ajaxSettings.url, reading jqXHR.status and thrownError, opting out with global: false, and comparing global events with per-request .fail().

01

Global

AjaxEvent

02

document

Bind target

03

Errors

Fail only

04

settings

Filter URL

05

thrownError

Status text

06

Since 1.7

.on() API

Introduction

When your page loads data with $.ajax(), $.get(), $.post(), or .load(), jQuery broadcasts lifecycle events to listeners on document. The ajaxError event is your global “something went wrong” hook — it fires only when a request fails, such as a 404 Not Found, a 500 server error, or a network timeout.

The official jQuery API distinguishes the ajaxError event (bound with .on("ajaxError") since 1.7) from the deprecated .ajaxError() method used in older code. Unlike ajaxComplete, which runs for both success and failure, ajaxError is outcome-specific — successful requests trigger ajaxSuccess instead. On failed requests, ajaxError runs before ajaxComplete.

Understanding the ajaxError Event

ajaxError is classified as an AjaxEvent — a global event triggered by jQuery’s Ajax transport, not a DOM event on a specific element. Whenever an Ajax request completes with an error, jQuery triggers ajaxError on document. Handlers receive four arguments: the native event object, the jqXHR object (same as the XMLHttpRequest wrapper returned by $.ajax()), ajaxSettings — the plain settings object used to create that request — and thrownError, the HTTP status text such as “Not Found” or “Internal Server Error.”

Every registered ajaxError handler runs for every failed qualifying request. To react only to certain URLs, inspect ajaxSettings.url inside the handler. Note: this handler is not called for cross-domain script or cross-domain JSONP requests.

💡
Beginner Tip

Bind once on $(document).on("ajaxError", fn) instead of adding a .fail() callback to every $.ajax() call when you need site-wide error logging or a global toast. Use global: false on requests that should not notify global listeners — for example, expected 404 checks or third-party widgets.

📝 Syntax

The modern jQuery API for the ajaxError global event:

1. Bind a handler — $(document).on("ajaxError" [, handler]) (since 1.7)

jQuery
$( document ).on( "ajaxError", handler )
  • handler — function invoked when any qualifying Ajax request fails: function( event, jqXHR, ajaxSettings, thrownError ) { ... }.
  • event — the jQuery event object.
  • jqXHR — the jqXHR / XMLHttpRequest object; read jqXHR.status for the HTTP status code.
  • ajaxSettings — settings passed to $.ajax() (url, type, data, context, etc.).
  • thrownError — textual HTTP status on HTTP errors, e.g. "Not Found" or "Internal Server Error".

2. Official demo — document handler + .load() to missing URL

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

  $( ".log" ).text( "Triggered ajaxError handler." );

} );



$( "button.trigger" ).on( "click", function() {

  $( "div.result" ).load( "ajax/missing.html" );

} );

3. Filter by URL — inspect ajaxSettings

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

  if ( settings.url == "ajax/missing.html" ) {

    $( "div.log" ).text( "Triggered ajaxError handler." );

  }

} );

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

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

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

5. Deprecated method — .ajaxError(handler)

jQuery
// Deprecated — use .on("ajaxError") instead

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

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

} );

The old .ajaxError(handler) binding method still works but is deprecated. Migrate to $(document).on("ajaxError", handler) for consistency with other events and reliable unbinding via .off().

Return value

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

⚡ Quick Reference

GoalCode
Bind global error handler$(document).on("ajaxError", fn)
Handler signaturefunction(event, jqXHR, ajaxSettings, thrownError)
Read HTTP status codejqXHR.status
Read status textthrownError (4th argument)
Filter by URLif (ajaxSettings.url === "/api/x")
Opt out of global events$.ajax({ url: "/x", global: false })
Per-request error handling$.ajax(...).fail(fn)
Remove handlers$(document).off("ajaxError")

📋 ajaxError vs .fail() vs ajaxComplete

Three ways to react when an Ajax request fails — choose the scope and outcome that match your task.

ajaxError
global

Document event — runs for every qualifying failed request on the page

.fail()
per call

jqXHR callback — runs once for a single $.ajax() invocation that fails

ajaxComplete
all outcomes

Global event — fires after success or error; runs after ajaxError on failures

ajaxSuccess
2xx only

Global event — fires only when the request succeeds (not on error)

Global event order on a failed request: ajaxStartajaxSendajaxErrorajaxCompleteajaxStop. On success, ajaxSuccess replaces ajaxError in step 3. ajaxStart and ajaxStop bracket the set of active global requests.

Examples Gallery

Examples 1, 3, and 4 follow the official jQuery API documentation. Examples 2 and 5 extend those patterns with URL filtering and a comparison with .fail(). Use the Try-it links to run each snippet in the browser.

📚 Official Patterns

Document-level handlers that observe every qualifying failed Ajax request.

Example 1 — Official Demo: Document Handler + .load() to Missing URL

Attach ajaxError on document, then trigger a failing Ajax request with .load() when the user clicks .trigger.

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

  $( ".log" ).text( "Triggered ajaxError handler." );

} );



$( "button.trigger" ).on( "click", function() {

  $( "div.result" ).load( "ajax/missing.html" );

} );
Try It Yourself

How It Works

.load() is implemented with $.ajax() internally, so a failed fetch triggers global Ajax events. The ajaxError handler on document runs when the server returns an error or the resource is missing.

Example 2 — Filter by settings.url

All ajaxError handlers run for every failed request — use ajaxSettings.url to narrow the callback to one endpoint.

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

jQuery passes the same settings object used in $.ajax(settings) as the third argument. Compare settings.url (or a custom property you add to settings) before updating the DOM or logging.

Example 3 — Official Demo: Append Error Message to #msg

Show a message in the page whenever any Ajax request fails.

jQuery
$( document ).on( "ajaxError", function( event, request, settings ) {

  $( "#msg" ).append( " Error requesting page " + settings.url + " " );

} );
Try It Yourself

How It Works

Because ajaxError fires for every global failed request, appending to #msg builds a running error log. Pair with URL filtering if you only want messages for specific endpoints.

📈 Status Details & Per-Request Comparison

Read thrownError and jqXHR.status, and compare global vs per-request error handling.

Example 4 — Read thrownError and jqXHR.status

Inspect the HTTP status code and status text when a request fails.

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

  $( "#details" ).html(

    "Status: " + jqXHR.status + " — " + thrownError +

    " (URL: " + settings.url + ")"

  );

} );
Try It Yourself

How It Works

On HTTP errors, jQuery passes the status text as thrownError and the numeric code on jqXHR.status. Use both for user-friendly error messages and logging.

Example 5 — Compare Global ajaxError Counter vs Per-Request .fail()

One global handler increments a page-wide counter; each $.ajax() call uses .fail() for its own error callback. Successful requests do not trigger ajaxError.

jQuery
var globalError = 0;

var perRequestFail = 0;



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

  globalError++;

  $( "#global-count" ).text( globalError );

} );



$( "#run-fail" ).on( "click", function() {

  $.ajax( "/api/missing" ).fail( function() {

    perRequestFail++;

    $( "#fail-count" ).text( perRequestFail );

  } );

  $.ajax( "/api/broken" ).fail( function() {

    perRequestFail++;

    $( "#fail-count" ).text( perRequestFail );

  } );

} );
Try It Yourself

How It Works

ajaxError centralizes cross-cutting error concerns (toasts, analytics). .fail() keeps error handling next to the call site — show a row-specific message, re-enable one button, etc. Only failed requests trigger ajaxError.

🚀 Common Use Cases

  • Error logging — append failed URL and status to a debug panel for every Ajax error during development.
  • Global toast notifications — show a user-friendly “Something went wrong” banner when any request fails, filtered by settings.url.
  • Analytics hooks — send error timing, URL, and jqXHR.status to monitoring when ajaxError fires.
  • Session expiry detection — watch for 401/403 responses globally and redirect to login.
  • Third-party isolation — set global: false on widget requests so expected failures do not trigger your global ajaxError logic.
  • Testing — assert that a test harness saw the expected number of failed requests via a global counter.

🧠 Global Ajax Event Order (Failed Request)

1

ajaxStart

First active global request begins — show a page-wide loading indicator.

ajaxStart
2

ajaxSend

Just before the request is sent — last chance to modify headers globally.

ajaxSend
3

ajaxError

Request failed — all registered ajaxError handlers execute with thrownError.

ajaxError
4

ajaxComplete

Always runs after step 3 — cleanup hook for success or error.

ajaxComplete
5

ajaxStop

Last active global request finished — hide the loading indicator.

📝 Notes

  • Bind with $(document).on("ajaxError", handler) since jQuery 1.7 — not the deprecated .ajaxError(handler) method.
  • Since jQuery 1.9, attach global Ajax handlers to document — not window.
  • ajaxError fires only on failure — successful requests trigger ajaxSuccess instead.
  • On failed requests, ajaxError runs before ajaxComplete.
  • Every registered handler runs for every failed qualifying request.
  • Requests with global: false do not trigger ajaxError.
  • Not called for cross-domain script or cross-domain JSONP requests.
  • Read the HTTP code with jqXHR.status and the status text from thrownError.
  • Differentiate requests with ajaxSettings.url, ajaxSettings.type, or custom settings properties.
  • For per-request error handling, prefer $.ajax(...).fail(fn) over a global handler with heavy filtering.

Browser Support

The ajaxError event is a jQuery global Ajax event — not a native DOM event. It works wherever jQuery’s Ajax transport runs: all browsers supported by your jQuery version. Bind with .on("ajaxError") since 1.7; attach handlers to document since 1.9.

jQuery 1.7+

jQuery ajaxError event

Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Requires jQuery Ajax — requests made with fetch() or 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 Universal

Bottom line: Safe in any jQuery project using $.ajax(), $.get(), $.post(), or .load(). Prefer .on('ajaxError') over deprecated .ajaxError().

Conclusion

The jQuery ajaxError event is the global “request failed” hook — it runs when any qualifying Ajax call completes with an error. Bind handlers on document with .on("ajaxError", fn), inspect ajaxSettings to filter by URL, and read jqXHR.status plus thrownError for error details.

Remember the event order on failures: ajaxError first, then ajaxComplete, then ajaxStop when no global requests remain. Use global: false to opt out. For logic tied to one call, chain .fail() on the jqXHR instead of growing a global handler.

💡 Best Practices

✅ Do

  • Bind on $(document).on("ajaxError", handler)
  • Filter with ajaxSettings.url when the handler should not run for every failed request
  • Pair with ajaxStart / ajaxStop for loading indicators
  • Use global: false for silent background or expected-failure requests
  • Remove handlers with $(document).off("ajaxError") on SPA teardown

❌ Don’t

  • Attach global handlers to window — use document since jQuery 1.9
  • Use deprecated .ajaxError(handler) in new code
  • Assume ajaxError fires on success — use ajaxSuccess or ajaxComplete
  • Rely on ajaxError for cross-domain script or JSONP requests
  • Duplicate per-request .fail() logic inside a global handler without filtering

Key Takeaways

Knowledge Unlocked

Six things to remember about ajaxError

Global, errors only, on document.

6
Core concepts
02

Errors

Fail only

Behavior
url 03

settings

Filter

ajaxSettings
404 04

thrownError

Status text

4th arg
off 05

global:false

Opt out

Private
.on 06

.on()

Not .ajaxError()

Modern

❓ Frequently Asked Questions

ajaxError is a global Ajax event (AjaxEvent) that fires whenever a qualifying jQuery Ajax request completes with an error — HTTP failures, transport errors, or parser errors. Bind handlers with $(document).on('ajaxError', handler) since version 1.7. Every registered handler runs for every failed request.
Since jQuery 1.9, all global Ajax event handlers — including ajaxError — must be bound to document, not window or other elements. Use $(document).on('ajaxError', fn) and $(document).off('ajaxError', fn) to remove them.
No. ajaxError runs only when a request fails. Successful responses trigger ajaxSuccess instead. Both are followed by ajaxComplete, which fires for success and error alike. Use ajaxError for global error logging; use ajaxSuccess when you only care about successful responses.
ajaxError is a global document event — one handler observes every qualifying failed request on the page. fail() is per-request on the jqXHR returned by $.ajax() — it runs only for that single call. Use global events for site-wide error UI; use fail() for request-specific error 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() callbacks still run normally.
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) for complete error details.
Did you know?

jQuery fires ajaxError for every registered handler — if three functions listen on document, all three run when one request fails. That differs from per-request .fail(), which runs only for the jqXHR it was chained to. On a failed request, ajaxError always runs before ajaxComplete. Cross-domain script and JSONP requests never trigger ajaxError.

Next: .ajaxError() Method

Learn the deprecated shorthand for binding global ajaxError handlers on document.

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