Skip to content

Instantly share code, notes, and snippets.

@jrburke
Created May 9, 2013 19:51
Show Gist options
  • Save jrburke/5550075 to your computer and use it in GitHub Desktop.
Save jrburke/5550075 to your computer and use it in GitHub Desktop.
quick json loader plugin
define({
load: function (id, require, onload, config) {
if (config.isBuild) {
return onload();
}
var xhr = new XMLHttpRequest();
xhr.open('GET', id + '?bust=' + Date.now(), true);
xhr.send(null);
xhr.onreadystatechange = function (evt) {
var status, err;
//Do not explicitly handle errors, those should be
//visible via console output in the browser.
if (xhr.readyState === 4) {
status = xhr.status;
if (status > 399 && status < 600) {
//An http 4xx or 5xx error. Signal an error.
err = new Error(url + ' HTTP status: ' + status);
err.xhr = xhr;
onload.error(err);
} else {
onload(JSON.parse(xhr.responseText));
}
}
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment