2011-04-03

OData from JavaScript - Netflix Genres

Microsoft’s WCF Data Services Team announced a new JavaScript library for OData in February. The documentation for the new library gives several code snippet examples, but doesn’t provide live examples. Here is a live example that gets all the genres in the Netflix catalog. The source code is here.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("jquery", "1"); // 1.5.2
    </script>
    <script type="text/javascript" src="datajs-0.0.2.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function () {
 
        // http://datajs.codeplex.com/documentation
 
        OData.defaultHttpClient.enableJsonpCallback = true;
 
        OData.read("http://odata.netflix.com/v1/Catalog/Genres",
        function (data, request) {
          var html = "";
          for (var i = 0; i < data.results.length; i++) {
            html += "<div>" + data.results[i].Name + "</div>";
          }
          document.getElementById("Genres").innerHTML = html;
        }, function (err) {
          alert("Error occurred " + err.message);
        });
 
      });
    </script>
    <title>Genres</title>
  </head>
  <body>
    <div id="Genres"></div>
  </body>
</html>