[英]jQuery ajax in ajax request
We have some code(simplified) that looks like bellow. 我们有一些看起来像下面的代码(简体)。 We run function
x
which does an ajax call. 我们运行功能
x
进行ajax调用。 When the call is done we call a different function recalculateOrderObjects
which also does an ajax call. 调用完成后,我们将调用另一个函数
recalculateOrderObjects
,该函数也会执行ajax调用。 When this one is completed, it should output the data that which is obtained via the second call. 完成此操作后,应输出通过第二个调用获得的数据。 However, what actually happens is that only the first ajax call is made and the second is not executed (or at least immediately goes to done) but does show the data obtained from the first call as the data obtained from the second one.
但是,实际上发生的是,仅进行了第一个ajax调用,而第二个ajax调用未执行(或至少立即执行了),但确实将从第一个调用获得的数据显示为从第二个调用获得的数据。
When running only the recalculateOrderObjects
function the function does work as expected. 仅运行
recalculateOrderObjects
函数时,该函数确实按预期工作。
Edit 1 编辑1
subscription
variable is a global subscription
变量是全局变量 recalculateOrderObjects
independent the function work when first x
is called and after that I call recalculateOrderObjects
independently the function will not work and shows the same behaviour as when called from `x.' recalculateOrderObjects
,函数在调用第一个x
时起作用,此后我独立调用recalculateOrderObjects
该函数将不起作用,并且表现出与从x调用时相同的行为。 Edit 2 编辑2
I tried the suggestion to use success
instead of done
as well. 我试图用暗示
success
,而不是done
的很好。 With the same result. 结果相同。
recalucateOrderObjects
is called succesfully, thought after one executing x
the whole ajax call in recalucateOrderObjects
is never requested again but instead thinks that it is succesfully executed. recalucateOrderObjects
被成功调用,以为执行一次x
之后,就不会再次请求recalucateOrderObjects
的整个ajax调用,而是认为它已成功执行。
function recalculateOrderObjects() {
$.ajax({
type: 'post',
url: url + "something",
data: {data: subscription}
})
.done(function (data) {
console.log('Data ' + data);
});
}
function x(){
jQuery.ajax({
type: "get",
dataType: "json",
url: url,
async: false
}).done(function (response) {
recalculateOrderObjects();
}
});}
x();
You can't get success responce by ".done" function. 您无法通过“ .done”功能获得成功响应。
"success" function gives you responce after ajax load. “成功”功能使您在ajax加载后做出响应。 Please try below code
请尝试以下代码
function recalculateOrderObjects() {
$.ajax({
type: 'post',
url: url + "something",
data: {data: subscription},
success: function(data) {
console.log('Data ' + data);
}
});
}
function x(){
jQuery.ajax({
type: "get",
dataType: "json",
url: url,
success: function(data) {
recalculateOrderObjects();
}
});
}
x();
OR 要么
function x(){
jQuery.ajax({
type: "get",
dataType: "json",
url: url,
success: function(data) {
$.ajax({
type: 'post',
url: url + "something",
data: {data: subscription},
success: function(data) {
console.log('Data ' + data);
}
});
}
});
}
x();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.