Last active
October 5, 2017 07:59
-
-
Save davidsan/4dc5e327d8ff1cbdeda7 to your computer and use it in GitHub Desktop.
Example of JSONP callback on Instagram Search API
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
<html> | |
<body> | |
<button id="btn" type="button">Search for jessicaalba on Instagram using JSONP</button> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/json2/20140204/json2.min.js"></script> | |
<script src="instagram.js"></script> | |
</body> | |
</html> |
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
var URI_INSTAGRAM_SEARCH = 'https://api.instagram.com/v1/users/search'; | |
// Your Instagram Client ID | |
var INSTAGRAM_CLIENT_ID = ''; | |
function callbackInstagram(data){ | |
var prettyJSON = JSON.stringify(data, null, 4); | |
output(prettyJSON); | |
} | |
function getInstagramSearch(query) { | |
var script = document.createElement('script'); | |
script.setAttribute('src', URI_INSTAGRAM_SEARCH+'?q='+query+'&client_id='+INSTAGRAM_CLIENT_ID+'&callback=callbackInstagram'); | |
document.body.appendChild(script); | |
} | |
function output(inp) { | |
document.body.appendChild(document.createElement('pre')).innerHTML = inp; | |
} | |
$(function(){ | |
var button = $('#btn'); | |
button.on('click', function(){ | |
getInstagramSearch("jessicaalba") | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, why create a script tag instead use an ajax call?