在jquery中获取最后一个ajax调用

[英]Get last ajax call in jquery


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

我想在我的代码中进行最后一次ajax调用。

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" .

但当我在我的控制台中看到它说“TypeError:$ .ajax.mostRecentCall is undefined”。

Thanks,

谢谢,

5 个解决方案

#1


7  

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

您可以注册一个全局ajaxComplete处理程序,每次AJAX调用完成时都会调用该处理程序。

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

有了这个,你可以模拟像Jasmine $ .ajax.calls.mostRecentCall()属性:

$(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.

在这种情况下,我将保存jqXHR对象,而不是传递给$ .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.

当然,请注意,在调用$ .ajax后不会立即填充它 - 在至少一个调用完成之前不会填充它。

#2


3  

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

我认为mostRecentCall函数来自Jasmine框架。您必须在代码中包含Jasmine。

mostRecentCall does not exist in jquery!

jquery中不存在mostRecentCall!

#3


0  

there is no such this like "$.ajax.mostRecentCall" in ajax jquery.

在ajax jquery中没有像“$ .ajax.mostRecentCall”这样的东西。

As PSR mentioned you could try to use a flag variable

正如PSR所提到的,你可以尝试使用标志变量

#4


0  

Adding an alert before logging will give enough time for the ajax call to be finished.(that should do the trick)

在记录之前添加警报将为ajax调用提供足够的时间来完成。(应该这样做)

if you don't want any alerts then you can use sleep() method

如果您不想要任何警报,那么您可以使用sleep()方法

#5


0  

There is nothing like

没有什么比这更像了

$.ajax.mostRecentCall

in Jquery. That is from Jasmine.js. Unless and until you add reference to Jasmine.js and follow the steps to spy on function, it will give you an error.

在Jquery。那是来自Jasmine.js。除非并且直到你添加对Jasmine.js的引用并遵循窥探函数的步骤,否则它将给你一个错误。

You may want to refer the documentation of Jasmin.js. Here's the link that shows how to use it.

您可能想参考Jasmin.js的文档。这是显示如何使用它的链接。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/06/06/2ffdb3baa630775d0d86d4c0e62bf091.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号