Last active
October 13, 2017 22:22
-
-
Save JoelLisenby/6f85396a912b43197f2094e7b5a0f3aa to your computer and use it in GitHub Desktop.
Simple Ajax Call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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