Skip to content

Instantly share code, notes, and snippets.

@therealFoxster
Created October 1, 2022 01:45
Show Gist options
  • Save therealFoxster/10d42285123498cdade8c46042d0b7bc to your computer and use it in GitHub Desktop.
Save therealFoxster/10d42285123498cdade8c46042d0b7bc to your computer and use it in GitHub Desktop.
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