How can I catch all ajax requests in javascript?

Is it possible to have a global event handler for ajax requests that is automatically called when any ajax request is returned?

I am interested in this because I am creating a greasemonkey script for the ajax site. In previous scenarios, I either ran the main function every few seconds or rewrote parts of the javascript site, both things are dirty.

+4
source share
2 answers

Not with regular XMLHttpRequest. Some libraries, such as jQuery, provide wrappers (e.g. ajaxComplete ). They will only run for ajax requests that also use a wrapper (e.g. jQuery.ajax ).

+2
source

using jQuery, I do this.

$(document).ready(function() { $("#contentLoading").hide(); $("#contentLoading").ajaxSend(function(r,s){ $(this).show(); $("#ready").hide(); }); $("#contentLoading").ajaxStop(function(r,s){ $(this).hide(); $("#ready").show(); }); 

Instead of hiding and showing things, you can start the generator.

0
source

Source: https://habr.com/ru/post/1308619/


All Articles