user12184452
user12184452

Reputation:

How to display API data by using multiple returned data from multiple API's?

I have successfully figured out how to display singular API data but have faced an issue. - I am trying to use the following API to return the data of "data" using the "following" shown in the next API example.

For this to be completed, two following API's are used.

Sorry, I am very stuck and have no idea what I have done wrong. Below is my attempted code, I hope this is helpful and explained well.

          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);
        })();

Upvotes: 0

Views: 342

Answers (1)

Jay Jordan
Jay Jordan

Reputation: 743

I was able to get results from your $.ajax requests.

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>

Upvotes: 1

Related Questions