jquery嵌套ajax调用格式化

jquery nested ajax calls formatting(jquery嵌套ajax调用格式化)

本文介绍了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();
        }
    });
}

这篇关于jquery嵌套ajax调用格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:jquery嵌套ajax调用格式化

基础教程推荐