Skip to content

Instantly share code, notes, and snippets.

@theClarkSell
Created July 28, 2011 11:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save theClarkSell/1111427 to your computer and use it in GitHub Desktop.
Save theClarkSell/1111427 to your computer and use it in GitHub Desktop.
Consuming an OData Service from jQuery
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="author" content="Clark Sell" />
<title>Consuming an OData Service from jQuery</title>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.6.2.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script> <!-- http://api.jquery.com/category/plugins/templates/ -->
<script src="/scripts/dsshows.js"></script>
</head>
<body>
<p>On Developer Smackdown</p>
<script id="dsListTemplate" type="text/html">
<ul>
<li><a href="${PublicUri}" target="blank">${Title}</a></li>
</ul>
</script>
<div id="ds_shows"></div>
</body>
</html>
jQuery(document).ready(function () {
var query = "http://developersmackdown.com/services/odata/Shows" //root uri
+ "?$select=ShowId,Title,PublicUri" //reduce the payload to what we need
+ "&$orderby=ShowId%20desc" //order by and sort by the latest shows
+ "&$top=5" //take the top 5
+ "&$format=json" //give me json
+ "&$callback=callback"; //this is my callback
jQuery.ajax({
dataType: 'jsonp',
url: query,
jsonpCallback: 'callback',
success: callback
});
function callback(result) {
var shows = result.d;
jQuery('#dsListTemplate').tmpl(shows).appendTo('#ds_shows');
}
});
<body>
<p>On Developer Smackdown</p>
<script id="dsListTemplate" type="text/html">
<ul>
<li><a href="${PublicUri}" target="blank">${Title}</a></li>
</ul>
</script>
<div id="ds_shows"></div>
</body>
<script>
$(document).ready(function () {
var query = "http://developersmackdown.com/services/odata/Shows" //root uri
+ "?$select=ShowId,Title,PublicUri" //reduce the payload to what we need
+ "&$orderby=ShowId%20desc" //order by and sort by the latest shows
+ "&$top=5" //take the top 5
+ "&$format=json" //give me json
+ "&$callback=callback"; //this is my callback
$.ajax({
dataType: 'jsonp',
url: query,
jsonpCallback: 'callback',
success: callback
});
function callback(result) {
var shows = result.d;
$('#dsListTemplate').tmpl(shows).appendTo('#ds_shows');
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment