Javascript 根据在另一个API上回收的数据从一个API请求数据

Javascript 根据在另一个API上回收的数据从一个API请求数据,javascript,api,Javascript,Api,我使用两个函数从API获取一些数据: 第一个请求每个周期的数据,第二个检查每个周期是否已完成付款 所有数据都放在一个公共表中。我的问题似乎是因为我在另一个函数中使用了一个函数。第二个函数只有在第一个函数完成后才能执行 var obj,obj2,dbParam,dbParam2,xmlhttp,xmlhttp2,myObj,myObj2,x,y,txt,txt2=“”; obj={表:“循环”,极限:10}; dbParam=JSON.stringify(obj); xmlhttp=新的XMLH

我使用两个函数从API获取一些数据: 第一个请求每个周期的数据,第二个检查每个周期是否已完成付款

所有数据都放在一个公共表中。我的问题似乎是因为我在另一个函数中使用了一个函数。第二个函数只有在第一个函数完成后才能执行

var obj,obj2,dbParam,dbParam2,xmlhttp,xmlhttp2,myObj,myObj2,x,y,txt,txt2=“”;
obj={表:“循环”,极限:10};
dbParam=JSON.stringify(obj);
xmlhttp=新的XMLHttpRequest();
//获取inputbox的值
//KT1试验地址KT19www5fiQNAiqTWrugTVLm9FB3th5DzH54
var KT1=$('#KT1').val();
xmlhttp.onreadystatechange=函数(){
obj2={表:“cycle2”,限值:100};
if(this.readyState==4&&this.status==200){
myObj=JSON.parse(this.responseText);
txt+=“”
txt+=“循环”
//txt+=“奖励”
txt+=“已支付”
txt+=“”
//使用API 1获取每个周期的数据
对于(myObj中的x){
//强制x到11以获得条件PaymentCycle=cycle
x=11;
cycle=myObj[x]。周期;
//余额=myObj[x]。余额/1000000;
//TotalReward=myObj[x]。奖励/1000000;
//立桩余额=myObj[x]。立桩余额/1000000;
//份额=余额/余额*100;
//委托人报酬=份额*总报酬/100;
//创建表的行
txt+=”;
txt+=“”+循环+“”;
//txt+=“”+Math.round(委托人奖励*100)/100+“”;
//在这里,循环正确地从106变为87
console.log(“功能之前的循环:+循环”);
//API2请求
dbParam2=JSON.stringify(obj2);
xmlhttp2=新的XMLHttpRequest();
xmlhttp2.onreadystatechange=函数(){
if(this.readyState==4&&(this.status==200 | | this.status==0)){
myObj2=JSON.parse(this.responseText);
//此处出错-始终获取上一个周期(87)的数据,而不是使用API1进行每个周期检查
//似乎只有在xmlhttp完成并为cycle(87)提供保存的最后一个值之后,才会执行此函数xmlhttp2
console.log(“函数后的循环:+循环”);
对于(变量y=0;y<30;y++){
//获取从106到90的付款周期
Block=myObj2[y]。类型。操作[0]。操作级别;
PaiementCycle=数学圆(Block/4096);
PaiementCycle=PaiementCycle-6;
//如果输入框中输入的数据=API 2的目的地地址以及API1和API2的循环相同,则
//此处循环始终=87(API1读数的最后一个值(在功能之前,循环从106变为87)。
//我真的不明白为什么
if(KT1==myObj2[y]。type.operations[0]。destination.tz&&PaiementCycle==cycle){
console.log(“获取一个”);
console.log(“PaiementCycle:+PaiementCycle”);
Paid=“//”+myObj2[y]。type.operations[0]。金额/1000000;
控制台日志(“已支付:+已支付);
txt+=“付款:”+已付款+”;
txt+=”;
//此函数期间保存的所有数据将在(x)的执行或结束后保存
console.log(“Txt//:”+Txt);
//document.getElementById(“demo2”).innerHTML=txt2;
}否则{
}//
}
//返回txt;
}否则{
//控制台日志(“不正常”);
}
};
xmlhttp2.open(“POST”https://api6.tzscan.io/v3/operations/tz1XynULiFpVVguYbYeopHTkLZFzapvhZQxs?type=Transaction&number=100“,对);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xmlhttp2.send(“x=“+dbParam2”);
}
txt+=”;
console.log(“Txt 2:+Txt”);
document.getElementById(“demo”).innerHTML=txt;
}
};
open(“POST”https://api6.tzscan.io/v3/delegator_rewards_with_details/“+KT1,正确);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xmlhttp.send(“x=”+dbParam);

您可以在这里看到我的代码笔,您可以很容易地看到我的函数xmlhttp2.onreadystatechange=function(){只在xmlhttp.onreadystatechange=function(){之后执行自己,而不是在从JSON文件获取每个数据之后尝试使用JavaScript。
一个简单的承诺是

是的,这似乎是去的解决方案,但我不明白;(尝试过,但仍然不起作用!我真的很难理解承诺!有人可以突出我?我也尝试过,但你的逻辑令人困惑。我希望有人会回答你。我的想法是创建一个表:第一个API用于完成前两列(周期、估计报酬)。然后,我运行第二个API来检查此周期是否已完成付款,并将其存储在第三列中。因此,每次更新一个周期的数据时,我都要运行第二个API来检查此周期是否已完成付款。这两个API的工作原理是:
var obj, obj2, dbParam,dbParam2, xmlhttp, xmlhttp2, myObj, myObj2, x, y, txt, txt2 = "";
    obj = { table: "cycle", limit: 10 };


    dbParam = JSON.stringify(obj);  
    xmlhttp = new XMLHttpRequest();

    // Get the value of the inputbox
    // KT1 adress for trial KT19www5fiQNAiqTWrugTVLm9FB3th5DzH54
    var KT1 = $('#KT1').val();

    xmlhttp.onreadystatechange = function() {



      obj2 = { table: "cycle2", limit: 100 };


      if (this.readyState == 4 && this.status == 200) {
        myObj = JSON.parse(this.responseText);
        txt += "<table><tr bgcolor=#000000 color=White>"
        txt += "<th>Cycle</th>"
        //txt += "<th>Reward</th>"
        txt += "<th>Paid</th>"
        txt += "</tr>"



              // Get the data of every cycle using API 1
              for (x in myObj) {
                // force x to 11 to get the condition PaymentCycle = cycle
                x = 11;
                cycle = myObj[x].cycle;
                //balance = myObj[x].balance/1000000;
                //TotalReward = myObj[x].rewards/1000000;
                //stakingBalance = myObj[x].staking_balance/1000000;
                //Share = balance/stakingBalance*100;
                //DelegatorReward = Share*TotalReward/100;

                // create line of the table
                txt += "<tr>";
                txt += "<td width=10% align=center>" + cycle + "</td>";
                //txt += "<td width=10% align=center>" + Math.round(DelegatorReward*100)/100 + "</td>";

                // here the CYCLE CHANGE CORRECTLY from 106 to 87
                console.log("Cycle before function: " + cycle);

                //API2 request
                dbParam2 = JSON.stringify(obj2);
                xmlhttp2 = new XMLHttpRequest();


                    xmlhttp2.onreadystatechange = function() {

                    if (this.readyState == 4 && (this.status == 200 || this.status == 0)) {

                      myObj2 = JSON.parse(this.responseText);

                      // ERROR HERE - ALWAYS GET THE DATA OF THE LAST CYCLE (87) instead of every cycle check with API1
                      // It seems that this fonction xmlhttp2 is executed only after xmlhttp is complete giving to cycle the last value of saved for cycle (87)
                      console.log("Cycle after function: " + cycle);

                       for (var y = 0; y < 30; y++) {
                              // Get the Paiement cycle which varies from 106 to 90
                              Block = myObj2[y].type.operations[0].op_level;
                              PaiementCycle = Math.round(Block/4096);
                              PaiementCycle = PaiementCycle - 6;

                              // If the Data entered in the input box = of the destination adress of API 2 and the cycle of API1 and API2 is the same then
                              // Here cycle is always = 87 (Last value of the API1 reading (before the function the cycle change from 106 to 87). 
                              // I really don't understand why
                              if (KT1 == myObj2[y].type.operations[0].destination.tz && PaiementCycle == cycle) { 
                                console.log("Get one");
                                console.log("Paiement Cycle : " + PaiementCycle);
                                Paid = "/////////////////" + myObj2[y].type.operations[0].amount/1000000;
                                console.log("Paid : " + Paid);

                                txt += "<td width=10% align=center>Paiement :" + Paid + "</td>";
                                txt += "</tr>";
                                // All the data saved during this function is saved after the execution or the boucle for(x)
                                console.log("Txt /////: " + txt);
                                      // document.getElementById("demo2").innerHTML = txt2;
                              } else {

                              }//

                        }
                     //return txt;
                    } else {
                      //console.log("Not Ok");
                    }
                    };
                xmlhttp2.open("POST", "https://api6.tzscan.io/v3/operations/tz1XynULiFpVVguYbYeopHTkLZFzapvhZQxs?type=Transaction&number=100", true);
                xmlhttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xmlhttp2.send("x=" + dbParam2);


        }
       txt += "</table>";

        console.log("Txt 2 : " + txt);
        document.getElementById("demo").innerHTML = txt;

      }
  };

xmlhttp.open("POST", "https://api6.tzscan.io/v3/delegator_rewards_with_details/" + KT1, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);