如何通过使用来自多个API的多个返回数据来显示API数据?

我已经成功地弄清楚了如何显示单个API数据,但是遇到了一个问题。 -我正在尝试使用以下API通过下一个API示例中显示的“跟随”来返回“数据”的数据。

要完成此操作,请使用以下两个API。

  • 用于返回“数据”,以进一步用于在相关API中检索“后续数据”。
  • 用于获取“数据”以访问先前的“后续数据”。

对不起,我非常困惑,不知道自己做错了什么。以下是我尝试的代码,希望这对您有所帮助并得到很好的解释。

          let response1 = await $.ajax({
            method: 'GET',url: '',});
          let response2 = await $.ajax({
            method: 'GET',url: ''
          });
          let response3 = await $.ajax({
            method: 'GET',url: ''
          });
          const result = response3.find(item => item.ID === response2.result === response1.result.codes)
          console.log(result.ID);
        })();
emheiyan 回答:如何通过使用来自多个API的多个返回数据来显示API数据?

我能够从您的$.ajax请求中获得结果。

let response1 = $.ajax({
  method: 'GET',url: 'https://api.postcodes.io/scotland/postcodes/FK15LD',});

let response2 = $.ajax({
  method: 'GET',url: 'https://data.parliament.scot/api/constituencies'
});

function response3(id) {

  return $.ajax({
    method: 'GET',url: 'https://data.parliament.scot/api/MemberElectionConstituencyStatuses/' + id
  });
}

let resp3 = response3(2);
resp3.then(function(res3) {
  console.log(res3);
});

// Or
// I'm not sure where you want to get the ID from for your third request,but

response1.then(function(res1) {
  // do something with res1
  response2.then(function() {
    // do something with res2
    let resp3 = response3(2); // or response3(res2[0].id);
    resp3.then(function(res3) {
      // do something with res3
      console.log(res3);
    });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

本文链接:https://www.f2er.com/3136251.html

大家都在问