首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >根据另一个API上的数据向另一个API请求数据

根据另一个API上的数据向另一个API请求数据
EN

Stack Overflow用户
提问于 2019-04-28 08:03:11
回答 1查看 98关注 0票数 0

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

所有数据都放在一个公用表中。我的问题似乎来自于我在另一个函数中使用函数的事实。第二个函数只有在第一个函数完成后才会自动执行。

https://codepen.io/anon/pen/XQOVVB?editors=1001

代码语言:javascript
复制
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);

您可以在这里看到我的代码,您可以很容易地看到我的函数xmlhttp2.onreadystatechange = function() {只在xmlhttp.onreadystatechange = function() {之后执行,而不是在从JSON文件中获取每个数据之后执行

EN

回答 1

Stack Overflow用户

发布于 2019-04-28 08:55:56

尝试使用JavaScript Promises。一个简单的承诺是here

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55885904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档