Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery AJAX Global Timeout (Entire Application) Im using $.get & $.post (can't use $.ajax)

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 ??

like image 799
Jagadeesh P Avatar asked Jun 05 '13 06:06

Jagadeesh P


People also ask

What is the default timeout for jQuery ajax?

The default value is 0 , which means there is no timeout.

Which of the following adds a timeout to an ajax request using jQuery?

This can be achieved by using jQuery setTimeout() function. This function executes the given Ajax code after some amount of given time.

Does ajax call have timeout?

The jQuery ajax timeout option is used to specifies that the number of milliseconds a request should wait for automatically being terminated. The jQuery ajax timeout option is a built-in option that is passed to the ajax() function in the jQuery.


1 Answers

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.

like image 133
ilyes kooli Avatar answered Sep 22 '22 15:09

ilyes kooli