r/StackoverReddit • • Jul 18 '24

Javascript JavaScript api question

Can a GET request have more than one link? I’m using an api that pulls one item per link but I need multiple links from the api for a single function.

Ex.

Xhttp.open(“GET”, “url”, true);

Can I add multiple links to “open” and apply them to the function?

Function ex. Function form results(event) {

If { radioOptionOne.checked && .radioOptionTwo.checked == true) { Response = JSON.parse(xhttp.response); Document.getElementById(“”).innerHTML = response.Title; }

Xhttp.open(“GET”, “url”, true); Xhttp.setRequestHeader (‘api key’, ‘key’);

Xhttp.setRequestHeader (‘api host’, ‘api url’);

Any help would truly be appreciated.

2 Upvotes

12 comments sorted by

View all comments

Show parent comment

3

u/heesell Jul 18 '24

Could you not make an array of those links, loop over each link and do a api call append items to a new array

async function call() {
  const urls = [
    "https://jsonplaceholder.typicode.com/todos/1",
    "https://jsonplaceholder.typicode.com/todos/2",
    "https://jsonplaceholder.typicode.com/todos/3"
  ]
  let arr = [];
  for (const url of urls) {
    const response = await fetch(url);
    const json = await response.json();
    arr.push(json);
  }
  console.log(arr);
}

call();