Handling Ajax errors
Introducing any kind of network interaction into an application brings along some degree of uncertainty. The user's connection could drop in the middle of an operation or a temporary server issue could interrupt communications. Because of these reliability concerns, we should always plan for the worst case and prepare for error scenarios.
The $.ajax() function can take a callback function named error to be called in these situations. In this callback, we should provide some kind of feedback to the user indicating that an error has occurred:
$(document).ready(function() {
var $ajaxForm = $('#ajax-form'),
$response = $('#response'),
noresults = 'There were no search results.',
failed = 'Sorry, but the request could not ' +
'reach its destination. Try again later.';
$ajaxForm.on('submit', function(event) {
event.preventDefault();
$.ajax({
url: 'http://book.learningjquery.com/api/',
dataType: 'jsonp',
data: {
...