weixin_33725126 2015-12-03 21:36 采纳率: 0%
浏览 20

嵌套Ajax调用理论

I've found many questions regarding users that requires to execute nested ajax call, but I can't understand if it is a good choice or not to nest them.

Most people tell to "divide" in multiple functions, for example:

function firstCall() {
    $.ajax({
        // [...]
        onSuccess: function(response){
            secondCall();
        }
    });
}

function secondCall() {
    $.ajax({
        // [...]
        onSuccess: function(response){
            thirdCall();
        }
    });
}

function thirdCall() {
    $.ajax({
        // [...]
        onSuccess: function(response){
            andSoOn();
        }
    });
}
  1. Is this the only solution?
  2. What are the main problems of nested ajax calls?

I'm developing a web application that requires a lot of ajax interation and sometimes I have to nest the ajax calls, for example to retrieve data that depends on first ajax call result in order to refresh DOM elements

  1. Should I move everything to the back-end?
  • 写回答

1条回答 默认 最新

  • 叼花硬汉 2015-12-03 21:44
    关注

    Is this the only solution?

    No, there is another way to handle ajax requests similar to this by using Promises, but functionality wise it is still the same as using callbacks.

    What are the main problems of nested ajax calls?

    Main downsides that I can think of with nested ajax calls is that if one call fails, it might interrupt the rest of the successive ajax calls, so this may create issues if not accounted for.

    评论

报告相同问题?