Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last ajax call in jquery

I want to get 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 is undefined" .

Thanks,

like image 341
Ancient Avatar asked Dec 20 '22 04:12

Ancient


2 Answers

You may register a global ajaxComplete handler that will be invoked every time an AJAX call finishes.

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'm saving the jqXHR object, rather than the exact set of parameters that was passed to $.ajax.

Note, of course, that this won't be populated immediately after $.ajax is called - it won't be filled until at least one call has finished.

like image 162
Alnitak Avatar answered Dec 22 '22 19:12

Alnitak


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

mostRecentCall does not exist in jquery!

like image 35
Sharky Avatar answered Dec 22 '22 19:12

Sharky