Last active
October 25, 2017 18:19
-
-
Save Luftare/8d6be9034530d4522f99e22ead8085b5 to your computer and use it in GitHub Desktop.
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
/* | |
Minimal AJAX get request method to fetch data. Example usage: | |
get('example.com/api/products/123') | |
.then(product => console.log(product.name)) | |
*/ | |
function get(url) { | |
return new Promise(res => { | |
let xmlHttp = new XMLHttpRequest(); | |
xmlHttp.onreadystatechange = () => { | |
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) res(JSON.parse(xmlHttp.responseText)); | |
} | |
xmlHttp.open('GET', url, true); | |
xmlHttp.send(); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment