Skip to content

Instantly share code, notes, and snippets.

@iMagesh
Created March 23, 2015 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iMagesh/6e0f334f7b72ed55ec79 to your computer and use it in GitHub Desktop.
Save iMagesh/6e0f334f7b72ed55ec79 to your computer and use it in GitHub Desktop.
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;
});
(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);
}
};
}
@iMagesh
Copy link
Author

iMagesh commented Mar 23, 2015

Javascript equivalent of jquery's jsonp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment