简体   繁体   English

多个ajax调用和数组变量

[英]multiple ajax call and array variables

I am running in to some interesting situation. 我遇到了一些有趣的情况。 on my application I have couple of situations. 在我的应用程序上,我有两种情况。 1. I have to grab data from two different sources.(for that i have used ajax call). 1.我必须从两个不同的来源获取数据。(为此,我使用了ajax调用)。 2. I have to manipulate those data comparing to each other. 2.我必须操纵那些相互比较的数据。 if both are equal than third array will gets the value input from first array first array. 如果两者相等,则第三个数组将从第一个数组的第一个数组获取值。 and Eventually i have to return the third value and work on my graphs. 最终,我必须返回第三个值并处理我的图。

so for that I have : 因此,我有:

 getData : function(){
    var bubbleArray= [];
    var companyData=[];
    var managerData =[];
    $.ajax({
        async: false,
        url: "data/companyData.json",
        dataType: "json",
        success: function (bubbleJsonData){
     $.each (bubbleJsonData.main.DATA_RECORD, function(index, response){
          if(response.C_HRS!=0&&response.D_CUST_HRS!=0){
    companyData.push([(response.C_HRS/442)*100, (response.D_CUST_HRS/442)*100, ((response.D_CUST_HRS/response.C_HRS)*100), response.C_HRS, response.D_CUST_HRS, response.CPY_NAME ]);
            }
            });
        },
    error: function(jqXHR, textStatus, errorThrown){
    alert("Error:"+ errorThrown);
            }
//ajax call to get the managerData. 
$.ajax({
            async: false,
            url: "data/managerData.json",
            dataType:"json",
            success: function(managerjsonData){
            $.each (managerjsonData.main.DATA _RECORD, function(index, responsedata){
            if(responsedata.CPY_NAME!=""){
            managerData.push([responseData.CPY_NAME]);
            }
            });
            },
            error: function(jqXHR, textStatus, errorThrown){
            alert("Error:"+ errorThrown);
            }

             });

    });

now, I have to compare the managerData. 现在,我必须比较managerData。 CPY_NAME with companyData.CPY_NAME if the match found generate the bubbleArray with the details of companyData means bubbleArray should have C_HRS, D_CUST_HRS,.......... if any help available form anybody would be highly appreciated CPY_NAME与companyData.CPY_NAME如果找到匹配项,则会生成带有companyData详细信息的bubbleArray,这意味着bubbleArray应该具有C_HRS,D_CUST_HRS,..........如果有人提供任何帮助,我们将不胜感激

You need to wait until both the requests finish and save their results in some variable, and then compare them. 您需要等待两个请求都完成,然后将它们的结果保存在某个变量中,然后再进行比较。

var yourAjaxRequests = [];
var jqXHR = $.ajax();
yourAjaxRequests.push(jqXHR);

$.when.apply($, yourAjaxRequests).done(function() {
    /* compare logic here */
);

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

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