Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
642 views
in Technique[技术] by (71.8m points)

jquery nested ajax calls formatting

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do something like this

function ajaxCall1(){
    $.ajax({
        success: function(){
            ajaxCall2();
        }
    });
}
function ajaxCall2(){
    $.ajax({
        success: function(){
            ajaxCall3();
        }
    });
}
function ajaxCall3(){
    $.ajax({
        success: function(){
            ajaxCall4();
        }
    });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...