简体   繁体   English

ajax请求依赖于前一个

[英]ajax request rely on the previous one

I want to do something like this: 我想做这样的事情:

$.ajax({
  url: SOMEWHERE,
  ...
  success: function(data){
    // do sth...
    var new_url = data.url;

    $.ajax({
      url: new_url,
      success: function(data){
        var another_url = data.url;

        // ajax call rely on the result of previous one
        $.ajax({
           // do sth
        })
      }
    })
  },
  fail: function(){
    // do sth...

    // ajax call too
    $.ajax({
      // config
    })
  }
})

the code looks awful for me. 代码对我来说太糟糕了。

I wonder how to make it looks pretty. 我不知道如何使它看起来很漂亮。 Some best practice? 一些最佳做法?

I would consider breaking it up, maybe something like this. 我会考虑将其分解,也许像这样。

function initialSuccessHandler(data) {
    $.ajax({url:data.url, success:secondarySuccessHandler});
}

function secondarySuccessHandler(data) {
    //do stuff
}

function initialFailHandler() {
    $.ajax({...});
}

$.ajax({url:"whatever.com", success:initialSuccessHandler, fail: initialFailHandler});

There's not a whole lot you can probably do about it other than if the success function are similar (just need different URL's to new AJAX calls for example) you might be able to define a common function to call recursively, like this: 除了成功函数类似(例如,与新的AJAX调用仅需要不同的URL)之外,您可能没有太多其他可以做的事情,您可以定义一个通用函数来递归调用,如下所示:

function do_something(data) {
    // some logic
    $.ajax({
        url: data.url,
        success: do_something(data);
        fail: function (){
            // handle failure
        }
    });
}

Use $.post instead of $.ajax that's lot easier and clean 使用$ .post而不是$ .ajax可以更轻松,更干净

$.post('yourAjaxUrl.com/index.html',(newVal:'val'), function(data) {
     $.post('yourSecondAjaxUrl.com/index.html',{newVal1:data}, function(data) {
              //do something 
     });
});

Or if you want to use GET request use like this. 或者,如果您想使用GET请求,请像这样使用。

$.get('yourAjaxUrl.com/index.html',(newVal:'val'), function(data) {
     $.get('yourSecondAjaxUrl.com/index.html',{newVal1:data}, function(data) {
              //do something 
     });
});

Other answers are mostly fine too as using functions in a lot of case will definitely help your code. 其他答案也都很好,因为在很多情况下使用函数肯定会帮助您的代码。 The problem of your function is that it's doing to much things all in once. 您的功能的问题在于,它一次执行很多事情。 Decreasing the complexity of the function will help a LOT (separating different action in different functions). 降低功能的复杂性将有助于LOT(在不同功能中分离不同的动作)。

There's some good training videos of Bocoup here which can help you decrease a function complexity: http://training.bocoup.com/screencasts/ 这里有一些Bocoup的良好培训视频,可以帮助您降低功能的复杂性: http ://training.bocoup.com/screencasts/

Although, a basic answer to the callback inferno: 虽然,对回调地狱的基本回答是:
You could use jquery Deffered who do a good job in certain case by preventing the "indentation pyramid of doom". 您可以使用jquery Deffered在某些情况下通过防止“厄运缩进金字塔”来做好工作。 (But won't decrease the complexity of your function) (但不会降低功能的复杂性)

$.ajax({
  url: SOMEWHERE
})
.pipe(function() {

  // First success callback
  return $.ajax({
    url: new_url
  });

}, function() {

  // First error callback
  $.ajax({
    // config
  });

  // we ain't returning anything so this end here.

})
.done(function( data ) {

  // Second success callback
  var another_url = data.url;

  // ajax call rely on the result of previous one
  $.ajax({
    // do sth
  })

});

Deferred can fit in a whole lot more of context, and learning about them is really useful. Deferred可以适应更多的上下文,了解它们确实很有用。 That's only the basic idea behind them. 那只是他们背后的基本想法。

Hope this help! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM