Skip to content

Instantly share code, notes, and snippets.

@Willovent
Created August 27, 2015 08:11
Show Gist options
  • Save Willovent/a86527d4c8abde2b0c3f to your computer and use it in GitHub Desktop.
Save Willovent/a86527d4c8abde2b0c3f to your computer and use it in GitHub Desktop.
vanilla post ajax with promise
postJSON = function(url,data){
return new Promise(function(resolve,reject)
{
var req = new XMLHttpRequest();
req.open('POST', 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