Skip to content

Instantly share code, notes, and snippets.

@rendro
Last active December 19, 2015 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rendro/5994602 to your computer and use it in GitHub Desktop.
Save rendro/5994602 to your computer and use it in GitHub Desktop.
ajax
var ajax = (function () {
var getAjaxRequest = function (callback) {
var ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange = function() {
if (ajaxRequest.readyState === 4) {
callback(ajaxRequest.responseText);
}
};
return ajaxRequest;
};
return {
get: function (url, callback) {
var ajaxRequest = getAjaxRequest(callback);
ajaxRequest.open("GET", url, true);
ajaxRequest.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
ajaxRequest.send(null);
},
post: function (url, data, callback) {
var ajaxRequest = getAjaxRequest(callback);
ajaxRequest.open("POST", url, true);
ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.send("data=" + encodeURIComponent(data));
}
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment