Created
August 27, 2015 08:11
-
-
Save Willovent/a86527d4c8abde2b0c3f to your computer and use it in GitHub Desktop.
vanilla post ajax with promise
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
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