Skip to content

Instantly share code, notes, and snippets.

@juliocesar
Last active August 8, 2016 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juliocesar/6f10855fae3496436971 to your computer and use it in GitHub Desktop.
Save juliocesar/6f10855fae3496436971 to your computer and use it in GitHub Desktop.
Very simple JSONP
// VSJONP ― Very Simple JSONP
// ==========================
//
// Usage:
// fetchJsonP({
// url: 'http://shit-no-cors.json',
// complete: function(response) {
// console.log(response);
// }
// });
(function() {
var fetchJsonP = function(options) {
var options = options ? options : {},
script = document.createElement('script');
functionName = 'jsonp' + Math.floor(Math.random() * 9999);
window[functionName] = function(response) {
if (options.complete) options.complete(response);
}
script.src = options.url + "&callback=" + functionName;
script.onload = function() {
document.body.removeChild(script);
delete window[functionName];
}
};
typeof module !== 'undefined' && 'exports' in module ?
module.exports = fetchJsonP :
window.fetchJsonP = fetchJsonP;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment