Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Last active August 29, 2015 13:56
Show Gist options
  • Save JosePedroDias/8928204 to your computer and use it in GitHub Desktop.
Save JosePedroDias/8928204 to your computer and use it in GitHub Desktop.
simple AJAX
var ajax = function(uri, cb) {
var xhr = new XMLHttpRequest();
//xhr.withCredentials = true; // uncomment to send cookies and stuff
xhr.open('POST', uri, true); // GET OR POST...
var cbInner = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
return cb(null, JSON.parse(xhr.response));
}
cb('error requesting ' + uri);
};
xhr.onload = cbInner;
xhr.onerror = cbInner;
xhr.send(null);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment