Created
July 25, 2013 11:26
-
-
Save hossambarakat/6078852 to your computer and use it in GitHub Desktop.
Interesting way to call ajax without dependent library
Source http://blogs.agilefaqs.com/2013/07/17/interesting-way-of-making-ajax-calls-from-your-javascript-without-using-any-framework-or-library/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+agilefaqs+%28Managed+Chaos%29
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
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