Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if and when any jQuery AJAX calls are made

I have a toolbar that exists on all my webpages that makes requests to a server side XML file regularly.

Some of the web pages also make requests to the same XML file, but more frequently. Ideally I would like to, where possible, combine this easily into a single request. Where the toolbar uses the same request that the page made (as the page refresh rate is greater than that of the toolbar)

Is there any way to tell if any jQuery AJAX calls have been made to a certain resources and, if so, be notified on success?

Update:

Based on Darin Dimitrov's answer I have tried the following:

$.ajaxSetup({
   success: function(){ console.log("woop"); }
 });

This never fires, I presume because the success handler is being overwritten when I make my other AJAX calls.

like image 461
Chris Avatar asked May 13 '11 14:05

Chris


People also ask

How do you check if all AJAX calls are completed?

jQuery ajaxStop() Method The ajaxStop() method specifies a function to run when ALL AJAX requests have completed. When an AJAX request completes, jQuery checks if there are any more AJAX requests. The function specified with the ajaxStop() method will run if no other requests are pending.

How do I know if jQuery AJAX is working?

You can use the setTimout() method which will execute your action after your given time. But it is not a better solution. jQuery has the inbuilt event handler to detect AJAX completion.

How do I know if AJAX request is successful?

You can check when exactly it returns "success" : // If successful, handle type chaining if ( status >= 200 && status < 300 || status === 304 ) { ... // If not modified if ( status === 304 ) { statusText = "notmodified"; ... // If we have data } else { try { ...


1 Answers

You could use the $.ajaxSetup() to subscribe for the common events.

like image 161
Darin Dimitrov Avatar answered Sep 24 '22 15:09

Darin Dimitrov