Skip to content

Instantly share code, notes, and snippets.

@ILoveBacteria
Last active March 10, 2023 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ILoveBacteria/32db228d58538a9849e28236d470498e to your computer and use it in GitHub Desktop.
Save ILoveBacteria/32db228d58538a9849e28236d470498e to your computer and use it in GitHub Desktop.
How to send a get request in JavaScript
async function getData(url) {
const headers = {} // Add headers here
const init = {
method: 'GET',
headers: new Headers(headers),
}
const response = await fetch(url, init);
if (response.ok) {
return await response.json();
}
throw new Error('The response code is not ok');
}
getData()
.then(resolved => console.log(resolved))
.catch(reject => console.log(reject));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment