External Data Access with jQuery


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

External Data Access with jQuery

  1. 1. External Data Access With jQuery and AJAX Doncho Minkov Telerik Mobile Development Course mobiledevcourse.telerik.com Technical Trainer http://www.minkov.it
  2. 2. Table of Contents <ul><li>RESTful Web Services </li></ul><ul><li>XML, JSON, RSS </li></ul><ul><li>Consuming REST with jQuery </li></ul><ul><li>Consuming Twitter REST with jQuery </li></ul><ul><li>Twitter @Anywhere </li></ul><ul><li>Twitter @Anywhere Features </li></ul><ul><li>Facebook API </li></ul>
  3. 3. RESTful Web Services Lightweight Architecture for Web Services
  4. 4. What is REST? &quot;Representational state transfer (REST) is a style of software architecture for distributed hypermedia systems such as the World Wide Web.&quot; http://en.wikipedia.org/wiki/Representational_State_Transfer <ul><li>Application state and functionality are resources </li></ul><ul><li>Every resource has an URI </li></ul><ul><li>All resources share a uniform interface </li></ul><ul><li>This natively maps to the HTTP protocol </li></ul>
  5. 5. RESTful Services <ul><li>One URI for a resource, multiple operations </li></ul><ul><ul><li>Add a new document &quot;RestTalk&quot; in category &quot;Code&quot; </li></ul></ul><ul><ul><ul><li>PUT http :// mysite.com/docs/Code/RestTalk </li></ul></ul></ul><ul><ul><li>Get the document / some page </li></ul></ul><ul><ul><ul><li>GET http :// mysite.com/docs/Code/RestTalk </li></ul></ul></ul><ul><ul><ul><li>GET http:// mysite.com/docs/Code/RestTalk/pages/3 </li></ul></ul></ul><ul><ul><li>Remove the document </li></ul></ul><ul><ul><ul><li>DELETE http :// mysite.com/docs/Code/RestTalk </li></ul></ul></ul><ul><ul><li>Retrieve metadata </li></ul></ul><ul><ul><ul><li>HEAD http :// mysite.com/docs/Code/RestTalk </li></ul></ul></ul>
  6. 6. XML, JSON, RSS Comparing the Common Service Data Formats
  7. 7. XML <ul><li>XML is markup-language for encoding documents in machine-readable form </li></ul><ul><ul><li>Text-based format </li></ul></ul><ul><ul><li>Consists of tags, attributes and content </li></ul></ul><ul><ul><li>Provide data and meta-data in the same time </li></ul></ul><?xml version=&quot;1.0&quot;?> <library> <book><title>HTML 5</title><author>Bay Ivan</author></book> <book><title>WPF 4</title><author>Microsoft</author></book> <book><title>WCF 4</title><author>Kaka Mara</author></book> <book><title>UML 2.0</title><author>Bay Ali</author></book> </library>
  8. 8. JSON <ul><li>JSON (JavaScript Object Notation) </li></ul><ul><ul><li>Standard for representing simple data structures and associative arrays </li></ul></ul><ul><ul><li>Lightweight text-based open standard </li></ul></ul><ul><ul><li>Derived from the JavaScript language </li></ul></ul>{ &quot;firstName&quot;: &quot;John&quot;, &quot;lastName&quot;: &quot;Smith&quot;, &quot;age&quot;: 25, &quot;address&quot;: { &quot;streetAddress&quot;: &quot;33 Alex. Malinov Blvd.&quot;, &quot;city&quot;: &quot;Sofia&quot;, &quot;postalCode&quot;: &quot;10021&quot; }, &quot;phoneNumber&quot;: [{ &quot;type&quot;: &quot;home&quot;, &quot;number&quot;: &quot;212 555-1234&quot;}, { &quot;type&quot;: &quot;fax&quot;, &quot;number&quot;: &quot;646 555-4567&quot; }] }, { &quot;firstName&quot;: &quot;Bay&quot;, &quot;lastName&quot;: &quot;Ivan&quot;, &quot;age&quot;: 79 }
  9. 9. RSS <ul><li>RSS (Really Simple Syndication) </li></ul><ul><ul><li>Family of Web feed formats for publishing frequently updated works </li></ul></ul><ul><ul><ul><li>E.g. blog entries, news headlines, videos, etc. </li></ul></ul></ul><ul><ul><li>Based on XML, with standardized XSD schema </li></ul></ul><ul><li>RSS documents (feeds) are list of items </li></ul><ul><ul><li>Each containing title, author, publish date, summarized text, and metadata </li></ul></ul><ul><li>Atom protocol aimed to enhance / replace RSS </li></ul>
  10. 10. RSS – Example <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <rss version=&quot;2.0&quot;> <channel> <title>W3Schools Home Page</title> <link>http://www.w3schools.com</link> <description>Free web building tutorials</description> <item> <title>RSS Tutorial</title> <link>http://www.w3schools.com/rss</link> <description>New RSS tutorial on W3Schools</description> </item> <item> <title>XML Tutorial</title> <link>http://www.w3schools.com/xml</link> <description>New XML tutorial on W3Schools</description> </item> </channel> </rss>
  11. 11. Consuming REST with jQuery How To Make It Work?
  12. 12. Consuming REST With jQuery <ul><li>Can be done with three methods </li></ul><ul><ul><li>$.ajax(…) </li></ul></ul><ul><ul><li>$.get(…) </li></ul></ul><ul><ul><li>$.post(…) </li></ul></ul><ul><li>$.get(…) and $.post(…) use $.ajax(…) under the hood </li></ul><ul><li>These methods load data from the server </li></ul><ul><ul><li>Using rest service </li></ul></ul><ul><ul><li>Return JSON, Atom, RSS </li></ul></ul>
  13. 13. Example of $.ajax(…) <ul><li>$.ajax({ </li></ul><ul><li>url: &quot;RestService.svc/students/all&quot;, </li></ul><ul><li>type: &quot;GET&quot;, </li></ul><ul><li>timeout: 5000, </li></ul><ul><li>dataType: &quot;json&quot;, </li></ul><ul><li>success: function (students) { </li></ul><ul><li>$('#loadStudentsButton').html(&quot;students loaded&quot;); </li></ul><ul><li>// do something with the students data </li></ul><ul><li>// visualize them, etc… </li></ul><ul><li>} </li></ul><ul><li>error: function (err) { </li></ul><ul><li>$('#loadStudentsButton').html(&quot;error: &quot; + err.status); </li></ul><ul><li>} </li></ul><ul><li>}); </li></ul>The path of the REST Service (should be on the same server) Request type ('GET' or 'POST') The type of data to expect(JSON,XML) In case the request is successful In case the request is unsuccessful <ul><li>Example of jQuery.ajax(…) get request </li></ul>
  14. 14. Example of $.ajax(…) <ul><li>$.ajax({ </li></ul><ul><li>url: &quot;RestService.svc/students/new&quot;, </li></ul><ul><li>type: &quot;POST&quot;, </li></ul><ul><li>timeout: 5000, </li></ul><ul><li>contentType: &quot;application/json&quot;, </li></ul><ul><li>data: JSON.stringify(student), </li></ul><ul><li>success: function () { </li></ul><ul><li>$('#createStudentButton').html(&quot;student created&quot;); </li></ul><ul><li>//reload the students </li></ul><ul><li>}, </li></ul><ul><li>error: function (err) { </li></ul><ul><li>$('#createStudentButton').html(&quot;error: &quot; + err.status); </li></ul><ul><li>} </li></ul><ul><li>}); </li></ul>The type of data to sent to the server (JSON, XML) We have a student object (in JSON) and should make it readable for the server <ul><li>Example of jQuery.ajax(…) post request </li></ul>
  15. 15. Consuming Our REST with jQuery Live Demo
  16. 16. Consuming Twitter REST with jQuery Lets Make Our Own Twitter?
  17. 17. Twitter Rest API <ul><li>First lets get familiar with the Twitter REST API </li></ul><ul><ul><li>We are given a set of methods to </li></ul></ul><ul><ul><ul><li>Get a number of tweets for a given user </li></ul></ul></ul><ul><ul><ul><li>Post a tweet using jQuery </li></ul></ul></ul><ul><ul><ul><li>Search tweets </li></ul></ul></ul><ul><ul><ul><li>Etc… </li></ul></ul></ul><ul><ul><li>First need to register a twitter app </li></ul></ul>
  18. 18. Twitter Rest API (2) <ul><li>Registering a Twitter App </li></ul><ul><ul><li>Go to http://dev.twitter.com </li></ul></ul><ul><ul><li>Register a App </li></ul></ul><ul><li>When your app is registered you get: </li></ul><ul><ul><li>Consumer key </li></ul></ul><ul><ul><ul><li>The key your app authorizes with </li></ul></ul></ul><ul><ul><li>Consumer secret </li></ul></ul><ul><ul><ul><li>Used for user authentication </li></ul></ul></ul>
  19. 19. Methods of Twitter REST API <ul><li>GET statuses/user_timeline </li></ul><ul><ul><li>Returns the 20 most recent statuses posted by the authenticating user </li></ul></ul><ul><ul><li>It is also possible to request another user's timeline by using </li></ul></ul><ul><ul><ul><li>The screen_name or user_id parameter </li></ul></ul></ul><ul><ul><li>The other users timeline will only be visible if they are not protected </li></ul></ul><ul><ul><ul><li>If the authenticating user's follow request was </li></ul></ul></ul>
  20. 20. Example of $.ajax(…) <ul><li>$.ajax({ </li></ul><ul><li>url : https://twitter.com/statuses/user_timeline/&quot;+ user+&quot;.json?callback=?&quot; </li></ul><ul><li>dataType : &quot;json&quot;, </li></ul><ul><li>timeout:5000, </li></ul><ul><li>success : function(data) { </li></ul><ul><li>//Visualize Tweets </li></ul><ul><li>}, </li></ul><ul><li>error : function() { </li></ul><ul><li> //Visualize Errors </li></ul><ul><li>}, </li></ul><ul><li>}); </li></ul><ul><li>Getting Tweets from Twitter with jQuery </li></ul>
  21. 21. Tweets Live Demo
  22. 22. Twitter @Anywhere How To Make It Easy?
  23. 23. <ul><li>What is Twitter @Anywhere ? </li></ul><ul><ul><li>An easy-to-deploy solution </li></ul></ul><ul><ul><ul><li>Bringing the Twitter communication platform </li></ul></ul></ul><ul><ul><li>Promotes a more engaged user base </li></ul></ul><ul><li>Can be used to add </li></ul><ul><ul><li>Follow Buttons , Hovercards , linkify Twitter usernames </li></ul></ul><ul><ul><li>Build integrations with &quot; Connect to Twitter &quot; </li></ul></ul>Example of $.ajax(…)
  24. 24. How to Use @Anywhere? <ul><li>With a registered App at dev.twitter.com </li></ul><ul><ul><li>You get a AppId </li></ul></ul><ul><li>In the head part of the app include </li></ul><ul><li>With anywhere.js included </li></ul><ul><ul><li>A twttr object and an anywhere function </li></ul></ul><ul><ul><ul><li>Used to make the magic with anywhere </li></ul></ul></ul><script src=&quot;http://platform.twitter.com/ anywhere.js?id=YOUR_APP_ID&v=1&quot;></script>
  25. 25. @Anywhere Example Live Demos
  26. 26. Search Widget <ul><li>new TWTR.Widget({ </li></ul><ul><li>version: 2, type: ' search ', search: , interval: 300, </li></ul><ul><li>title: 'It's a double rainbow', subject: 'Across the sky', </li></ul><ul><li>width: 250, height: 300, </li></ul><ul><li>theme: { </li></ul><ul><li> shell: {background:'# 70fd90 ',color: '# 000000 '}, </li></ul><ul><li> tweets: {background: '# c6fec7 ', color: '# 444444 ', links: '# 1985b5 '} </li></ul><ul><li>}, </li></ul><ul><li>features: { </li></ul><ul><li>scrollbar: true, </li></ul><ul><li>loop: true, </li></ul><ul><li>live: true, </li></ul><ul><li>behavior: 'default' </li></ul><ul><li> } </li></ul><ul><li>}).render().start(); </li></ul>
  27. 27. Tweets List Widget <ul><li>new TWTR.Widget({ </li></ul><ul><li>version: 2, type: ' profile ', search: , interval: 300, </li></ul><ul><li>title: 'It's a double rainbow', subject: 'Across the sky', </li></ul><ul><li>width: 250, height: 300, </li></ul><ul><li>theme: { </li></ul><ul><li> shell: {background:'# 70fd90 ',color: '# 000000 '}, </li></ul><ul><li> tweets: {background: '# c6fec7 ', color: '# 444444 ', links: '# 1985b5 '} </li></ul><ul><li>}, </li></ul><ul><li>features: { </li></ul><ul><li>scrollbar: true, </li></ul><ul><li>loop: true, </li></ul><ul><li>live: true, </li></ul><ul><li>behavior: 'default' </li></ul><ul><li> } </li></ul><ul><li>}).render().start(); </li></ul>
  28. 28. Twitter Widgets Live Demo
  29. 29. Twitter @Anywhere Features Login/Logout, Tweet, etc.
  30. 30. Login – Logout <ul><li>twttr.anywhere(function (T) { </li></ul><ul><li>T(&quot;#login&quot;).connectButton({ </li></ul><ul><li> authComplete: function(user) { </li></ul><ul><li>//something to do after authentication </li></ul><ul><li> }, </li></ul><ul><li>}); </li></ul><ul><li>}); </li></ul><ul><li>Login: </li></ul>function logOut() { twttr.anywhere.signOut(); } <ul><li>Logout </li></ul>
  31. 31. Login/Logout Live Demo
  32. 32. Features <ul><li>Tweet Box </li></ul>twttr.anywhere(function (T) { T(&quot;#tbox&quot;).tweetBox(); }); <ul><li>Follow Button </li></ul>twttr.anywhere(function (T) { T('#follow-donchominkov').followButton(&quot;donchominkov&quot;); });
  33. 33. Twitter @Anywhere Live Demo
  34. 34. Facebook API Lets Play a Little with Facebook
  35. 35. Facebook JavaScript SDK <ul><li>Facebook provides a SDK for JavaScript </li></ul><ul><ul><li>To use the functionality of Facebook </li></ul></ul><ul><li>Should register an app </li></ul><ul><ul><li>Through phone number </li></ul></ul><ul><ul><li>With credit card </li></ul></ul><ul><ul><li>Absolutely free </li></ul></ul><ul><ul><ul><li>Made for precautions </li></ul></ul></ul>
  36. 36. Facebook API Live Demo
  37. 37. External Data Access