Skip to content

Instantly share code, notes, and snippets.

@JoelLisenby
Last active October 13, 2017 22:22
Show Gist options
  • Save JoelLisenby/6f85396a912b43197f2094e7b5a0f3aa to your computer and use it in GitHub Desktop.
Save JoelLisenby/6f85396a912b43197f2094e7b5a0f3aa to your computer and use it in GitHub Desktop.
Simple Ajax Call
/**
Example Ajax Call
**/
var ExampleClass = function() {
var ec = this; // for access inside the ajax response function.
ec.init = function() {
ec.requestJSON( 'http://jsfiddle.net/echo/jsonp/?banana=delicious', ec.processData );
};
ec.processData = function() {
ec.result = JSON.parse(this.responseText);
if(!ec.result.error) {
console.log('ajax success!',ec.result);
} else {
console.log('ajax error');
}
};
ec.requestJSON = function(url, loadCallback) {
var oReq = new XMLHttpRequest();
oReq.open("GET", url);
oReq.send();
oReq.addEventListener("load", loadCallback);
};
ec.init();
};
new ExampleClass();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment