-
-
Save chiqui3d/caba08d78fa15b27acad27d133cd03a5 to your computer and use it in GitHub Desktop.
AJAX requests in ES6 using fetch()
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
// ES6 Fetch docs | |
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch | |
fetch('https://some.url.com') | |
.then(response => { | |
if (response.ok) { | |
return Promise.resolve(response); | |
} | |
else { | |
return Promise.reject(new Error('Failed to load')); | |
} | |
}) | |
.then(response => response.json()) // parse response as JSON | |
.then(data => { | |
// success | |
}) | |
.catch(function(error) { | |
console.log(`Error: ${error.message}`); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment