Skip to content

Instantly share code, notes, and snippets.

@DrFrankenstein
Created October 2, 2011 05:26
Show Gist options
  • Save DrFrankenstein/1257096 to your computer and use it in GitHub Desktop.
Save DrFrankenstein/1257096 to your computer and use it in GitHub Desktop.
Json fetcher in JavaScript
function JsonFetcher(url) {
this.url = url;
this.request = new XMLHttpRequest();
this.request.addEventListener("readystatechange",
goog.bind(this._onReadyStateChange, this));
}
JsonFetcher.prototype = {
url: "",
request: null,
callback: null,
_onReadyStateChange: function JsonFetcher$_onReadyStateChange() {
if(this.request.readyState == readyStates.DONE)
{
var jsonobject = goog.json.parse(this.request.responseText);
this.callback(jsonobject);
}
},
fetch: function JsonFetcher$fetch(callback) {
this.callback = callback;
this.request.open("GET", this.url);
this.request.send();
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment