Skip to content

Instantly share code, notes, and snippets.

@rgs1
Created October 24, 2014 06:19
Show Gist options
  • Save rgs1/bb15c886a4c2e8e3c4c5 to your computer and use it in GitHub Desktop.
Save rgs1/bb15c886a4c2e8e3c4c5 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;
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('https://www.sugarlabs.org/tjs/package.json', function(data) {
alert('Got this: ' + JSON.stringify(data));
}, function(status) {
alert('Something went wrong.');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment