jquery嵌套ajax调用格式化

2022-01-12 00:00:00 formatting jquery ajax

我需要根据上一次调用的数据连续进行 6 次 ajax 调用.我将每个调用嵌套在上一个调用的成功中.我的问题是有什么好的方法来格式化代码,这样它就不会在我的编辑器中出现一百万行?

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();
        }
    });
}

相关文章