简体   繁体   English

如何使用回调操作来自多个异步 ajax 异步调用的数据?

[英]How do I manipulate data coming from multiple async ajax async calls with callback?

I have the following piece of code that make async ajax calls with callback.我有以下一段代码,它使用回调进行异步 ajax 调用。

My first main issue is that is that when i do data manipulation using callback functions setA,setB,... the returned values from ajax calls are not always present and I end doing operations on unidentified variables from "dataFromAjax".我的第一个主要问题是,当我使用回调函数 setA,setB,... 进行数据操作时,ajax 调用的返回值并不总是存在,我结束对来自“dataFromAjax”的未识别变量的操作。 Is there a way to perform the operation in setB only when i know that i have some data in dataFromAjax['firstCall'] which was set by function setA ?只有当我知道我在 dataFromAjax['firstCall'] 中有一些由函数 setA 设置的数据时,有没有办法在 setB 中执行操作?

Second question is there a way to simplify the code without having to create a new success function for every data manipulation?第二个问题有没有一种方法可以简化代码而不必为每个数据操作创建一个新的成功函数?

dataFromAjax = {};

function makeACall(url,successCallBack,errorCallback){
   $.ajax({ 
          type: 'GET', 
          async: true,
          url: url, 
          success: successCallBack,
          error: errorCallback
          });
}

makeACall(url,setA,errorAjax)
makeACall(url,setB,errorAjax)
makeACall(url,setC,errorAjax)
makeACall(url,setD,errorAjax)

function setA(ajaxData){
   dataFromAjax['firstCall'] = ajaxData;       
}

function setB(ajaxData){
  dataFromAjax['secondCall'] =  dataFromAjax['firstCall'] + ajaxData;       
}

function setC(ajaxData){
  dataFromAjax['thirdCall'] =  dataFromAjax['secondCall'] / ajaxData;       
}

You can try with $.when function, here is documentation https://api.jquery.com/jquery.when/ Basically your code could looks like in example:您可以尝试使用$.when函数,这里是文档https://api.jquery.com/jquery.when/基本上您的代码可能如下所示:

$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).done(function( a1, a2 ) {
  // a1 and a2 are arguments resolved for the page1 and page2 ajax requests, respectively.
  // Each argument is an array with the following structure: [ data, statusText, jqXHR ]
  var data = a1[ 0 ] + a2[ 0 ]; // a1[ 0 ] = "Whip", a2[ 0 ] = " It"
  if ( /Whip It/.test( data ) ) {
    alert( "We got what we came for!" );
  }
});

In your case something like (depends on what you want achieve)在您的情况下(取决于您想要实现的目标)

$.when($ajax.(url), $.ajax(url), $.ajax(url), $.ajax(url)).then(success, error);

function success(a, b, c, d) { return (a+b)/c; }
function error() { /* error handling */ }    

If you want to check if property exists in the object (made it more consistent).如果要检查对象中是否存在属性(使其更加一致)。

if (dataFromAjax.firstCall) {
  dataFromAjax.secondCall =  dataFromAjax.firstCall + ajaxData; 
}

I'm not sure if I understand but if you don't want to make a separate success callback for every ajax call you can make a default one and remove an argument.我不确定我是否理解,但如果您不想为每个 ajax 调用进行单独的成功回调,您可以创建一个默认回调并删除一个参数。

function defaultSuccess(ajaxData) {
    // do what you want
}

function makeACall(url, errorCallback){
   $.ajax({ 
          type: 'GET', 
          async: true,
          url: url, 
          success: defaultSuccess,
          error: errorCallback
          });
}

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

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