Last active
August 29, 2015 13:56
-
-
Save JosePedroDias/8928204 to your computer and use it in GitHub Desktop.
simple AJAX
This file contains 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
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