pics/public/js/ajax.js
Aaron van Geffen ab0e4efbcb Initial commit.
This is to be the new HashRU website based on the Aaronweb.net/Kabuki CMS.
2017-11-09 17:00:22 +01:00

39 lines
1.0 KiB
JavaScript

function HttpRequest(method, url, payload, callback, context) {
if (!window.XMLHttpRequest) {
return;
}
var request = new XMLHttpRequest();
var async = typeof callback !== 'undefined';
if (async) {
request.onreadystatechange = function() {
if (request.readyState !== 4) {
return;
}
if (request.responseText !== null && request.status === 200) {
var obj = JSON.parse(request.responseText);
if (obj.error) {
alert(obj.error);
return;
}
else {
callback(obj, context);
}
}
};
}
if (method === 'get') {
request.open('GET', url, async);
request.send(null);
} else {
request.open('POST', url, async);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.send(payload);
}
return request;
};