Skip to content

Instantly share code, notes, and snippets.

@hossambarakat
Created July 25, 2013 11:26
Show Gist options
  • Save hossambarakat/6078852 to your computer and use it in GitHub Desktop.
Save hossambarakat/6078852 to your computer and use it in GitHub Desktop.
script type="text/javascript">
simpleAJAXLib = {
init: function () {
this.fetchViaJSONP('your_url_goes_here');
},
fetchViaJSONP: function (url) {
url += '?format=jsonp&jsonp_callback=simpleAJAXLib.handleResponse';
document.getElementsByTagName('body')[0].appendChild(this.jsTag(url));
},
jsTag: function (url) {
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', url);
return script;
},
handleResponse: function (results) {
// do the necessary stuff; for example
document.getElementById('demo').innerHTML = "Result = " + (results.error ? "Internal Server Error!" : results.response);
}
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment