Created
October 1, 2022 01:45
-
-
Save therealFoxster/10d42285123498cdade8c46042d0b7bc to your computer and use it in GitHub Desktop.
This file contains 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 getJSON(url, callback) { | |
try { | |
const response = await fetch(url, { | |
method: 'GET', | |
mode: 'cors', | |
headers: { | |
'Content-Type': 'application/json' | |
} | |
}); | |
const json = await response.json(); | |
if (callback) callback(json); | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
// function getJSON(url, callback) { | |
// fetch(url, { | |
// // https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#supplying_request_options | |
// method: 'GET', // *GET, POST, PUT, DELETE, etc. | |
// mode: 'cors', // no-cors, *cors, same-origin | |
// headers: { | |
// 'Content-Type': 'application/json' | |
// } | |
// }) | |
// .then(response => response.json()) | |
// .then(json => callback(json)) | |
// .catch(error => console.error(error)); | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment