13

I have created the spring web application with jquery. For every ajax call im using $.get or $.post (NOT $.ajax).

But I didn't handle any ajax timeouts or ajax error. Now my application is deployed in the server, I can't make any major changes.

Pls help me how to handle the AJAX Timeout for the entire web application and i should alert("Ajax Timeout") globally. additionally whatever the ajax error, i should alert the error message to the user.

I heard about $.ajaxSetup and $.ajaxError
But How to use them in my web application, Do i need to add it in document.ready ??

Community
  • 1
  • 1
Jagadeesh P
  • 141
  • 1
  • 1
  • 4

3 Answers3

32

You can attach an ajaxError handler to document

$(function(){
    $(document).ajaxError(function(event, request, settings) {
         //Do whatever
    });
});

Edit: To set Ajax timeout globally, you have to use ajaxSetup

$.ajaxSetup({
    timeout: 2000 //Time in milliseconds
});

Note:$.ajaxSetup should be set before other Ajax calls.

ilyes kooli
  • 11,959
  • 14
  • 50
  • 79
0

You can use $.ajaxSetup({timeout: 100});

-1

Here is how to set-up the ajaxError in your application.

Do this once inside the document ready.

$(document).ajaxError(function (event, xhr) {
    console.log(xhr.status + ': ' + xhr.statusText);
});

The xhr.status is the http status code returned, the most common 404 (not found), 201 (created), 403 (forbidden), for a list of these status codes refer to: http://www.w3.org/Protocols/HTTP/HTRESP.html.

Esteban
  • 3,108
  • 3
  • 32
  • 51