Skip to content

Instantly share code, notes, and snippets.

@pimbrouwers
Last active January 26, 2018 14:44
Show Gist options
  • Save pimbrouwers/67694ef25369c7cc87e10650c5344d70 to your computer and use it in GitHub Desktop.
Save pimbrouwers/67694ef25369c7cc87e10650c5344d70 to your computer and use it in GitHub Desktop.
AJAX helpers with jQuery
module.exports = new Ajax();
function Ajax() { };
Ajax.prototype.get = function (url, callback, error) {
var req = this.makeRequest('GET', url, callback, error);
req.send();
};
Ajax.prototype.post = function (url, data, callback, error) {
var req = this.makeRequest('POST', url, callback, error);
req.send(data);
};
Ajax.prototype.makeRequest = function (method, url, callback, error) {
var req = new XMLHttpRequest();
req.open(method, url, true);
req.onreadystatechange = function () {
if (req.readyState == 4 && req.status >= 200 && req.status < 400) {
callback(req.responseText);
}
else if (error) {
error();
}
};
req.onerror = function () {
if (error) error();
};
return req;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment