Skip to content

Instantly share code, notes, and snippets.

@duanckham
Last active January 19, 2018 15:56
Show Gist options
  • Save duanckham/e5b690178b759603b81c to your computer and use it in GitHub Desktop.
Save duanckham/e5b690178b759603b81c to your computer and use it in GitHub Desktop.
AJAX
var ajax = function(url, data) {
var wrap = function(method, cb) {
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(data ? JSON.stringify(data) : null);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status > 0)
cb(xhr);
}
return xhr;
};
return {
get: function(cb) {
return wrap('GET', cb);
},
post: function(cb) {
return wrap('POST', cb);
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment