使用.done的嵌套AJAX调用 [英] Nested AJAX Calls using .done
问题描述
我对AJAX还是很陌生,但是从事的项目需要ajax调用来验证特定值,然后如果第一个返回期望值,则再进行一次ajax调用.我正在尝试实现.done/.fail模型,但是找不到阻止两个调用同时发生的方法,而不是一旦第一个调用完成并成功就阻止了这两种方法.
I'm fairly new to AJAX, but working on a project that requires an ajax call to validate a specific value, then make another ajax call if the first returns the expected value. I am trying to implement the .done/.fail model, but can't find a way to prevent both calls from happening simultaneously, rather than once the first call is done and successful.
以下代码将两次调用ajaxCall函数,但同时调用而不是连续调用.我研究了很多代码,包括嵌套AJAX调用,jQuery摆脱了嵌套的Ajax函数和$.when.done回调不起作用,但似乎没有一个适合我的实际情况,或者也许我只是不理解代码.无论哪种方式,我都无法找到解决方案,任何帮助将不胜感激!
The following code will call the ajaxCall function twice, but concurrently rather than consecutively. I have researched a ton of code, including Nested AJAX Calls, jQuery getting rid of nested ajax functions, and $.when.done callback isn't working, but none seem to fit my exact scenario, or maybe I just don't understand the code. Either way, I haven't been able to find a solution, and any help will be much appreciated!
var xReturn = ajaxCall("1");
xReturn.done(function(msg){
console.log("callback "+msg+" successful");
// if successful, place second call
if(parseInt(msg)==1)
xReturn = ajaxCall("2");
});
function ajaxCall(mop){
return $.ajax({
url: "getItem.php",
type: "POST",
data: {code: '<?php echo $code; ?> ', op:mop}
});
}
看来 promise 可能是可行的方法,但是在这种情况下,我无法确定如何使用它们.在此先感谢您指出正确方向的任何指针.
It seems like promises may be the way to go, but I can't wrap my head around how to use them in this scenario. Thanks in advance for any pointers in the right direction.
更新:
我进行了一系列测试,结果各不相同.为了进行昨晚的最终测试,我每次在ajaxCall("2");
之后直接放置一个console.log(msg);
,每次生成的味精始终为"1"时,使我相信呼叫无法正常进行.这个结果告诉我xReturn.done(function(msg)...
仅被调用一次,但是我认为每次ajax调用都会被调用.
I ran through a battery of tests with different results. For my final test last night, I placed another console.log(msg);
directly after ajaxCall("2");
Each time the resulting msg was always "1", leading me to believe the calls were not happening properly. This result tells me that xReturn.done(function(msg)...
is only being called once, but I thought it would be called with each ajax call.
有了这些新信息,我将在今晚进行其他测试并报告.
谢谢
With this new information, I will perform additional testing tonight and report back.
Thanks
推荐答案
您需要将.done()
方法绑定到每个承诺. xReturn.done()
将功能绑定到该诺言.
You need to bind a .done()
method to each promise. xReturn.done()
binds a function to that promise.
当您执行xReturn = ajaxCall("2");
时,您将 替换 使用不同的对象.此对象没有绑定的.done()
方法.
When you do xReturn = ajaxCall("2");
, you are replacing xReturn
with a different object. This object does not have a .done()
method bound to it.
您需要将.done()
绑定到每个诺言,但这不会自动发生.
You need to bind .done()
to each promise, that doesn't happen automatically.
var xReturn = ajaxCall("1");
// This binds the callback to this *specific* promise
xReturn.done(ajaxDone);
function ajaxCall(mop){
return $.ajax({
url: "getItem.php",
type: "POST",
data: {code: '<?php echo $code; ?> ', op:mop}
});
}
function ajaxDone(msg){
console.log("callback "+msg+" successful");
// if successful, place second call
if(parseInt(msg)==1){
xReturn = ajaxCall("2");
// Bind a callback to this *new* object
xReturn.done(ajaxDone);
}
}
这篇关于使用.done的嵌套AJAX调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!