gregnnylf94 gregnnylf94 - 1 month ago 14
Ajax Question

Less repetitive error handling jquery

I have a page with a lot of jquery requests.
I always add something like this to my calls...

error: function(xhr, ajaxOptions, thrownError)
{
alert('Error in AJAX response. Please see console log for details.');

console.log(thrownError);
console.log("readyState: " + xhr.readyState);
console.log("responseText: "+ xhr.responseText);
console.log("status: " + xhr.status);
}


Question: Is there a simpler less repetitive way to do this? Rather than adding it to each request.

Answer Source

Check out $.ajaxSetup(). You can do something like this once, and it will apply to all subsequent ajax calls you make via jQuery:

$.ajaxSetup({
    error: function () {
        alert('error');
    }
});