gregnnylf94 gregnnylf94 -3 years ago 155
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("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:

    error: function () {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download