Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Last active February 27, 2016 03:12
Show Gist options
  • Save JosePedroDias/2d4bf89ccb4e5aaee9a4 to your computer and use it in GitHub Desktop.
Save JosePedroDias/2d4bf89ccb4e5aaee9a4 to your computer and use it in GitHub Desktop.
AJAX advanced
function ajax(o) {
'use strict';
var xhr = new XMLHttpRequest();
if (o.withCredentials) { xhr.withCredentials = true; }
xhr.open(o.method || 'GET', o.url, true);
var cbInner = function() {
if (xhr.readyState === 4 && xhr.status > 199 && xhr.status < 300) {
return o.cb(null, JSON.parse(xhr.response));
}
o.cb('error requesting ' + o.url);
};
xhr.onload = cbInner;
xhr.onerror = cbInner;
xhr.send(o.body || null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment