1
0
Fork 0

Add js http helper

This commit is contained in:
Gregory Eremin 2015-10-28 22:37:06 +03:00
parent 8c587201f4
commit 4edd072a56
1 changed files with 43 additions and 0 deletions

43
dashboard/js/http.js Normal file
View File

@ -0,0 +1,43 @@
try {
new ActiveXObject("Msxml2.XMLHTTP");
getXHR = function() {
return new ActiveXObject("Msxml2.XMLHTTP");
}
} catch (e) {
try {
new ActiveXObject("Microsoft.XMLHTTP");
getXHR = function() {
return new ActiveXObject("Microsoft.XMLHTTP");
}
} catch (e) {
if (typeof XMLHttpRequest !== 'undefined') {
getXHR = function() {
return new XMLHttpRequest();
}
} else {
alert("Something went really wrong!");
console.log("XHR is not available");
}
}
}
function getURL(url, params, callback) {
if (Object.keys(params).length > 0) {
var pairs = [];
for (key in params) {
pairs.push(key +'='+ encodeURIComponent(params[key]))
}
url += '?'+ pairs.join('&');
}
var xhr = getXHR();
xhr.open('GET', url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if(xhr.status === 200) {
callback(JSON.parse(xhr.responseText));
}
}
};
xhr.send(null);
}