Created
January 11, 2018 14:30
-
-
Save luelista/2f5308d419e6b9dfc6aee844e6197127 to your computer and use it in GitHub Desktop.
Simple HTTP request wrapper for XMLHttpRequest
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
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