Skip to content

Instantly share code, notes, and snippets.

@luelista
Created January 11, 2018 14:30
Show Gist options
  • Save luelista/2f5308d419e6b9dfc6aee844e6197127 to your computer and use it in GitHub Desktop.
Save luelista/2f5308d419e6b9dfc6aee844e6197127 to your computer and use it in GitHub Desktop.
Simple HTTP request wrapper for XMLHttpRequest
function http(url, method, data){
return new Promise(function(resolve, reject) {
var xhr;
if(method == undefined) method = data ? "POST" : "GET";
try {
xhr = new XMLHttpRequest();
} catch (e) {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
resolve(xhr.responseText);
} else {
reject(xhr);
}
}
}
xhr.error = function(e) {
reject(xhr);
}
xhr.open(method, url, true);
xhr.send(data);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment