Skip to content

Instantly share code, notes, and snippets.

@fguisso
Last active November 28, 2015 17:58
Show Gist options
  • Save fguisso/6a7cd2e2b41686b05cd7 to your computer and use it in GitHub Desktop.
Save fguisso/6a7cd2e2b41686b05cd7 to your computer and use it in GitHub Desktop.
JSONP with APIRest
// The callback funciton
function yourFunction (data) {
//console.log(data);
}
// create <script>
var myScript = document.createElement('SCRIPT');
// set the src URL with ?callback=yourFunction
myScript.src = 'http://example.com/api/user?callback=yourFunction';
// get the <head>
var head = document.getElementsByTagName('head')[0];
// insert your <script> before <head>
head.insertBefore(myScript, head.childNodes[0]);
// For test, change the url http://swapi.co/api/people/1/ (Star Wars API)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment