Last active
November 17, 2020 19:54
-
-
Save Willovent/4b9aa0b467d70f2a04d0 to your computer and use it in GitHub Desktop.
ajax with promise Vanilla
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
getJSON = function(url,data){ | |
return new Promise(function(resolve,reject) | |
{ | |
var req = new XMLHttpRequest(); | |
req.open('GET', url, true); | |
req.onreadystatechange = function () { | |
if (req.readyState == 4) { | |
if(req.status == 200) | |
resolve(JSON.parse(req.responseText)); | |
else | |
reject(Error(req.statusText)); | |
} | |
}; | |
req.onerror = function() { | |
reject(Error("network error")); | |
}; | |
req.send(data); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment