Q1. You can use .ajaxError() as follows:
$(document).ajaxError(function() { $('#msg').text('Connection error').addClass('err'); });
... or if the #msg element #msg present all the time, you can do this:
$('#msg').ajaxError(function() { $(this).text('Connection error').addClass('err'); });
Q2. You can use the handler arguments that ajaxError passes ajaxError , it uses this format: handler(event, XMLHttpRequest, ajaxOptions, thrownError) , something like this:
$(document).ajaxError(function(event, request, options, error) { $('#msg').addClass('err') .text('Connection error: ' + error + ' when connecting to ' + options.url); });
Note: in previous versions of jQuery this would be done via $.ajaxError() , since 1.7, which is no longer needed, you will need to connect it to the jQuery object you want, or document , if you do not care about a specific element.
source share