Skip to content

Instantly share code, notes, and snippets.

@MickaelCruzDB
Created July 1, 2015 14:13
Show Gist options
  • Save MickaelCruzDB/652869337b7bc3487594 to your computer and use it in GitHub Desktop.
Save MickaelCruzDB/652869337b7bc3487594 to your computer and use it in GitHub Desktop.
var getJSON = function(url, successHandler, errorHandler) {
var xhr = typeof XMLHttpRequest != 'undefined'
? new XMLHttpRequest()
: new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('get', url, true);
xhr.onreadystatechange = function() {
var status;
var data;
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-readystate
if (xhr.readyState == 4) { // `DONE`
status = xhr.status;
if (status == 200) {
data = JSON.parse(xhr.responseText);
successHandler && successHandler(data);
} else {
errorHandler && errorHandler(status);
}
}
};
xhr.send();
};
getJSON('http://icontrol.herokuapp.com/api/product/1/?format=json', function(data) {
document.write(data.activated)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment