jQuery的嵌套Ajax调用格式化 [英] jquery nested ajax calls formatting
本文介绍了jQuery的嵌套Ajax调用格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个要求,使基于从previous呼叫数据连续6 Ajax调用。我嵌套在previous调用成功每次调用。我的问题是有什么好的方法来格式化code,使其不结束了一百万行穿过我的编辑?
I have a requirement to make 6 ajax calls in succession based on data from the previous call. I am nesting each call in the success of the previous call. my question is what are some good ways to format the code so that it doesnt end up a million rows across my editor?
$.ajax({
type: "POST",
url: "someScript/someScript.php",
data: form + "&func=build",
success: function (result) {
if (result == "ok")
{
$.ajax({
type: "POST",
url: "someScript/someScript.php",
data: form + "&func=someOtherFunc",
success: function (result) {
if (result == "ok")
{
$.ajax({
type: "POST",
url: "someScript/someScript.php",
data: form + "&func=someOtherFunc",
success: function (result) {
if (result == "ok")
{
.....and so on
}
})
}
})
})
}
})
忽略括号,语法心不是对这个问题很重要。
ignore brackets, syntax isnt important for this question.
推荐答案
您可以做这样的事情
function ajaxCall1(){
$.ajax({
success: function(){
ajaxCall2();
}
});
}
function ajaxCall2(){
$.ajax({
success: function(){
ajaxCall3();
}
});
}
function ajaxCall3(){
$.ajax({
success: function(){
ajaxCall4();
}
});
}
这篇关于jQuery的嵌套Ajax调用格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文