Last active
March 10, 2023 19:33
-
-
Save ILoveBacteria/32db228d58538a9849e28236d470498e to your computer and use it in GitHub Desktop.
How to send a get request in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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