Created
March 23, 2015 08:53
-
-
Save iMagesh/6e0f334f7b72ed55ec79 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| Lib.ajax.getJSON({ | |
| url: 'https://api.twitter.com/1/statuses/user_timeline.json?&screen_name=gabromanato&callback=?&count=1', | |
| type: 'jsonp' | |
| }, function(tweet) { | |
| document.querySelector('#tweet').innerHTML = tweet[0].text; | |
| }); |
This file contains hidden or 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
| (function() { | |
| var Lib = { | |
| ajax: { | |
| xhr: function() { | |
| var instance = new XMLHttpRequest(); | |
| return instance; | |
| }, | |
| // code continues | |
| } | |
| }; | |
| window.Lib = Lib; | |
| })() | |
| getJSON: function(options, callback) { | |
| var xhttp = this.xhr(); | |
| options.url = options.url || location.href; | |
| options.data = options.data || null; | |
| callback = callback || | |
| function() {}; | |
| options.type = options.type || 'json'; | |
| var url = options.url; | |
| if (options.type == 'jsonp') { // JSONP | |
| window.jsonCallback = callback; // Now our callback method is globally visible | |
| var $url = url.replace('callback=?', 'callback=jsonCallback'); | |
| var script = document.createElement('script'); | |
| script.src = $url; | |
| document.body.appendChild(script); | |
| } | |
| xhttp.open('GET', options.url, true); | |
| xhttp.send(options.data); | |
| xhttp.onreadystatechange = function() { | |
| if (xhttp.status == 200 && xhttp.readyState == 4) { | |
| callback(xhttp.responseText); | |
| } | |
| }; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Javascript equivalent of jquery's jsonp