Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know if jQuery has an Ajax request pending?

I'm having some problems with a jQuery control we made. Suppose you have a dropdownlist that allows you to enter the ID of the item you're looking for, and when you press ENTER or lose focus in a textbox it validates via jQuery that the ID you entered is correct, showing an alert if it doesn't.

The thing is that when an ordinary user enters an invalid value in it and loses focus by pressing the submit button, the jQuery post returns after the submit of the form has been sent.

Is there any way that I can check if there's any Async request processing by jQuery so that I do not allow the form submit?

like image 796
sabanito Avatar asked Nov 30 '09 22:11

sabanito


People also ask

How do I know if AJAX request is running jQuery?

You need to attach . ajaxStop() to the document to detect when all AJAX requests get completed. Use global: false option in the AJAX requests if you don't want to detect by . ajaxStop() .

How to verify that an AJAX request is completed?

If you are using xmlhttp object for ajax call then when xmlhttp. readyState==4 then it is consider as ajax request is completed.

What is the difference between jQuery get () and jQuery AJAX ()?

get() executes an Ajax GET request. The returned data (which can be any data) will be passed to your callback handler. $(selector). load() will execute an Ajax GET request and will set the content of the selected returned data (which should be either text or HTML).


2 Answers

$.active returns the number of active Ajax requests.

More info here

like image 185
sivabudh Avatar answered Nov 12 '22 00:11

sivabudh


You could use ajaxStart and ajaxStop to keep track of when requests are active.

like image 29
ceejayoz Avatar answered Nov 12 '22 02:11

ceejayoz