Skip to content

Instantly share code, notes, and snippets.

@Willovent
Last active November 17, 2020 19:54
Show Gist options
  • Save Willovent/4b9aa0b467d70f2a04d0 to your computer and use it in GitHub Desktop.
Save Willovent/4b9aa0b467d70f2a04d0 to your computer and use it in GitHub Desktop.
ajax with promise Vanilla
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