Get last ajax call in jquery

I want to get the last ajax call made in my code.

here is my code

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="Scripts/jquery-1.7.1.min.js"></script> <script> function getCreateAccount() { $.ajax({ type: "GET", url: "/Account/Register/", contentType: "application/json; charset=utf-8", dataType: "json" }); console.log($.ajax.mostRecentCall.args[0]); } </script> </head> <body> </body> </html> 

but when I see in my console, it says: "TypeError: $ .ajax.mostRecentCall - undefined".

Thanks,

+4
source share
5 answers

You can register the ajaxComplete global handler, which will be called every time the AJAX call ends.

With this, you can emulate something like the Jasmine $.ajax.calls.mostRecentCall() property:

 $(document).ajaxComplete(function(ev, jqXHR, settings) { $.ajax.mostRecentCall = jqXHR; }); 

In this case, I save the jqXHR object, and not the exact set of parameters that was passed to $.ajax .

Please note that this will not be filled immediately after calling $.ajax - it will not be filled until at least one call completes.

+7
source

I think the mostRecentCall function is from the Jasmine framework. You must include Jasmine in your code.

mostRecentCall does not exist in jquery!

+3
source

there is no such type of "$ .ajax.mostRecentCall" in ajax jquery.

As mentioned in the PSR, you can try using a flag variable

0
source

Adding a warning before logging will give enough time to complete the ajax call. (this should do the trick)

if you don't want any warnings you can use the sleep () method

0
source

Nothing is better

 $.ajax.mostRecentCall 

in jQuery. This is from Jasmine.js. If and until you add a link to Jasmine.js and follow the instructions for checking the function, this will lead to an error.

You can refer to the Jasmin.js documentation . Here is a link that shows how to use it.

0
source

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


All Articles