AJAX Error Handling
AJAX Error Handling: free step-by-step lesson with examples, common mistakes, and interview tips — part of jQuery Tutorial on Toolliyo Academy.
On this page
jQuery Tutorial · Lesson 46 of 100
AJAX Error Handling
Setup & DOM ✓ → Events Effects AJAX → Perf & Integrate → Ship & Projects
Events Effects AJAX · 2 — Interact · ~6 min · AJAX and APIs
What is this?
Failures include network errors, HTTP 4xx/5xx, timeouts, and parsererrors. .fail(xhr, status, error) distinguishes them so QueryVerse can show useful messages.
Why should you care?
Silent failures make ops teams distrust the UI — always surface something actionable.
See it live — copy this example
Examples include the jQuery 3.7 CDN. Paste into an HTML file or use Run Example to preview.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>QueryVerse</title>
<style>
body { font-family: system-ui, sans-serif; margin: 1.25rem; }
.box { padding: .75rem; border: 1px solid #ccc; border-radius: 8px; margin: .5rem 0; }
.muted { color: #666; }
button { margin-right: .35rem; margin-top: .35rem; }
input, select, textarea { margin: .25rem 0; }
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: .4rem .55rem; text-align: left; }
.hidden { display: none; }
.row-odd { background: #f7f7f7; }
.active { font-weight: 700; }
.toast { background: #111; color: #fff; padding: .5rem .75rem; border-radius: 6px; }
</style>
</head>
<body>
<button type="button" id="ok">Good URL</button>
<button type="button" id="bad">404 URL</button>
<pre id="out" class="box muted"></pre>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$(function () {
function explain(xhr, status, err) {
return JSON.stringify({ status: status, http: xhr.status, err: String(err) }, null, 2);
}
$('#ok').on('click', function () {
$.getJSON('https://jsonplaceholder.typicode.com/posts/1')
.done(function (p) { $('#out').text('OK: ' + p.title); })
.fail(function (x, s, e) { $('#out').text(explain(x, s, e)); });
});
$('#bad').on('click', function () {
$.getJSON('https://jsonplaceholder.typicode.com/posts/999999')
.done(function () { $('#out').text('unexpected'); })
.fail(function (x, s, e) { $('#out').text(explain(x, s, e)); });
});
});
</script>
</body>
</html>
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- Map 401 to login redirect, 403 to permission toast, 500 to retry later.
- always() clears spinners whether done or fail.
Practice next
- Run Good URL and Bad URL.
- Compare http status values.
- Add .always to reset a Loading label.
- Centralize error toasts in one helper.
- Retry idempotent GETs once.
Remember
fail receives xhr/status/error. Branch on HTTP code. always cleans UI.
Friendly failure copy
An IRCTC-like agent QueryVerse booking panel must say “seat map unavailable” instead of failing quietly.
Outcome: fail handlers translate status into operator-ready text.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!