Last active
January 26, 2018 14:44
-
-
Save pimbrouwers/67694ef25369c7cc87e10650c5344d70 to your computer and use it in GitHub Desktop.
AJAX helpers with jQuery
This file contains hidden or 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
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