How do you handle Ajax errors in jQuery?
<p>Page</p>
$.getJSON('missing.json')
.done(function () { console.log('ok'); })
.fail(function (xhr, status, error) {
console.log('Failed: ' + xhr.status + ' ' + error);
})
.always(function () { console.log('Request finished'); });
Console output
Failed: 404 Not Found
Request finished
Also set a timeout, show a message to the user and log the details. See jQuery Ajax.
Learn more about the similar topics:
| Tutorials |
|---|
| jQuery load() Method |
| jQuery Ajax: ajax(), get(), post() and getJSON() |
| jQuery Deferred, Promises and $.when() |
| Exercises & Assignments |
|---|
| Write jQuery code to load JSON data with Ajax and show it in a list |
| Write jQuery code to submit a form with Ajax |