Skip to content

Instantly share code, notes, and snippets.

@jvnlwn
Created February 21, 2014 18:16
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 jvnlwn/9139967 to your computer and use it in GitHub Desktop.
Save jvnlwn/9139967 to your computer and use it in GitHub Desktop.
<!-- SOME NOTES VIA ARI PICKER -->
<!doctype html>
<html>
<head>
<title>JSONP</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<!-- <script type="text/javascript" src="https://www.govtrack.us/api/v2/role?current=true"></script> -->
<!-- gives the following error: SyntaxError: Unexpected token : -->
<!-- same error you'd get from a simple expression {'abc' : 'abc'} -->
<!-- SO.... the return must be formatted in jsonp -->
<script type="text/javascript" src="https://www.govtrack.us/api/v2/role?current=true&format=jsonp&callback=joe"></script>
<!--the above way of requesting from an API needs a callback function -->
<script type="text/javascript">
function joe(data) {
console.log('data from joe callback: ', data)
}
</script>
<!-- here is your regular jsonp request via $.getJSON -->
<!-- $.getJSON('https://www.govtrack.us/api/v2/role?current=true&format=jsonp&callback=?') -->
<!-- and NOW THE MAGIC!!!!!! -->
<!-- you'd use the the below example, so that you're not just geting/fetching/saving
from the API 1 time, as you are above. you wana put the callback in a a file that says when to hit the API -->
<script type="text/javascript">
// now specifically with this api the above request will not work because of the jQuery caching feature . . so must set cache to true using an ajax call
$.ajax({
url: 'https://www.govtrack.us/api/v2/role?current=true&format=jsonp&callback=?',
cache: true,
dataType: 'jsonp',
success: function(data) {console.log('data from ajax success callback: ', data)}
})
// or you'd use a .get/.fetch/.save
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment