简体   繁体   English

处理多个AJAX调用的结果

[英]Manipulating the results of multiple AJAX Calls

I'm performing two separate AJAX calls and I'd ultimately like for the results to be in the form of a number variable that I can manipulate. 我正在执行两个单独的AJAX调用,最终我希望结果以我可以操纵的数字变量的形式出现。 I've wrapped the execution of the functions within $(function() in an attempt to wait until both of the AJAX functions have returned their value so as not to begin to do the math before the results are returned, but it appears that's not working. 我将函数的执行包装在$(function()内,以尝试等待两个AJAX函数都返回它们的值,以便在返回结果之前不开始做数学运算,但是似乎不是工作。

How can I ensure that the results are returned from two separate AJAX calls before the function manipulates their results? 在函数处理结果之前,如何确保从两个单独的AJAX调用返回结果?

 // Collect Data Point P function myCallbackP(result) { var p = Math.round(result/3); $('#past').html(p); } fooP(myCallbackP); function fooP (callback){ $.ajax({ url: 'https://' + company + '.teamwork.com/' + actionP, headers: {"Authorization": "BASIC " + window.btoa(key)}, }).done(function(response){ callback((response['todo-items']).length); }) } //Collect Data Point F function myCallbackF(result) { var f = (result); $('#future').html(f); } fooF(myCallbackF); function fooF (callback){ $.ajax({ url: 'https://' + company + '.teamwork.com/' + actionF, headers: {"Authorization": "BASIC " + window.btoa(key)}, }).done(function(response){ callback((response['todo-items']).length); }) } //Math up data point P and F $(function() { var v = myCallbackP(); var y =myCallbackP; var z = v/y; console.log(z); $('#ratio').html(z); console.log('success?'); console.log( "ready!" ); }); 

I suggest you use jQuery Deferred and Promises like below 我建议您使用jQuery DeferredPromises如下所示

 var ajax1 = fooP(); function fooP() { var defObj = $.Deferred(); $.ajax({ url: 'https://' + company + '.teamwork.com/' + actionP, headers: { "Authorization": "BASIC " + window.btoa(key) }, }).done(function(response) { defObj.resolve(response); }); return defObj.promise(); } var ajax2 = fooF(); function fooF() { var defObj = $.Deferred(); $.ajax({ url: 'https://' + company + '.teamwork.com/' + actionF, headers: { "Authorization": "BASIC " + window.btoa(key) }, }).done(function(response) { defObj.resolve(response); }); return defObj.promise(); } // when both calls are done $.when(ajax1, ajax2).done(function(data1, data2) { var p = Math.round(data1 / 3); $('#past').html(p); var f = (data2); $('#future').html(f); var z = p / f; console.log(z); $('#ratio').html(z); console.log('success?'); console.log("ready!"); }); 

You can use $.when() 您可以使用$.when()

// Collect Data Point P
function myCallbackP(result) {
  var p = Math.round(result / 3);
  $('#past').html(p);
}

function fooP(callback) {
  return $.ajax({
    url: 'https://' + company + '.teamwork.com/' + actionP,
    headers: {
      "Authorization": "BASIC " + window.btoa(key)
    }
  })
}

//Collect Data Point F
function myCallbackF(result) {
  var f = (result);
  $('#future').html(f);
}

function fooF(callback) {
  return $.ajax({
    url: 'https://' + company + '.teamwork.com/' + actionF,
    headers: {
      "Authorization": "BASIC " + window.btoa(key)
    }
  })
}

//Math up data point P and F
$(function() {
  $.when(fooP(), fooF())
  .then(function(p, f) {
    console.log('success?');
    myCallbackP(p[0]["todo-items"].length);
    myCallbackF(f[0]["todo-items"].length);
     var v = +$("#past").html();
     var y = +$("#future").html();
     var z = v / y;
     console.log(z);
    $('#ratio').html(z);
  })
  .fail(function(jqxhr, textStatus, errorThrown) {
     console.log(errorThrown);
  });

  console.log("ready!");
});

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

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