External Data External Data Access Access With jQuery and AJAX With jQuery and AJAX Doncho Minkov Doncho Minkov Telerik Mobile Development Course Telerik Mobile Development Course mobiledevcourse.teleri k.com Technical Trainer Technical Trainer http://www.minkov.it
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
External Data External Data AccessAccess
With jQuery and AJAXWith jQuery and AJAX
Doncho MinkovDoncho Minkov
Telerik Mobile Development CourseTelerik Mobile Development Coursemobiledevcourse.telerik.com
Table of ContentsTable of Contents RESTful Web ServicesRESTful Web Services XML, JSON, RSSXML, JSON, RSS Consuming REST with jQueryConsuming REST with jQuery Consuming Twitter REST with Consuming Twitter REST with
jQueryjQuery Twitter @AnywhereTwitter @Anywhere Twitter @Anywhere FeaturesTwitter @Anywhere Features Facebook APIFacebook API
RESTful Web RESTful Web ServicesServicesLightweight Architecture for Web Lightweight Architecture for Web
ServicesServices
What is REST?What is REST?
"Representational state transfer "Representational state transfer
(REST) is a style of software (REST) is a style of software
architecture for distributed architecture for distributed
hypermedia systems such as the World hypermedia systems such as the World
RSS (Really Simple Syndication)RSS (Really Simple Syndication) Family of Web feed formats forFamily of Web feed formats for
publishing frequently updated workspublishing frequently updated works
E.g. blog entries, news headlines, videos, E.g. blog entries, news headlines, videos, etc.etc.
Based on XML, with standardized XSD Based on XML, with standardized XSD schemaschema
RSS documents (feeds) are list of itemsRSS documents (feeds) are list of items Each containing title, author, publish date, Each containing title, author, publish date,
summarized text, and metadatasummarized text, and metadata Atom protocol aimed to enhance / replace Atom protocol aimed to enhance / replace
RSSRSS 9
RSS – ExampleRSS – Example
10
<?xml version="1.0" encoding="utf-8" ?><?xml version="1.0" encoding="utf-8" ?><rss version="2.0"><rss version="2.0"><channel><channel> <title>W3Schools Home Page</title><title>W3Schools Home Page</title> <link>http://www.w3schools.com</link><link>http://www.w3schools.com</link> <description>Free web building tutorials</description><description>Free web building tutorials</description> <item><item> <title>RSS Tutorial</title><title>RSS Tutorial</title> <link>http://www.w3schools.com/rss</link><link>http://www.w3schools.com/rss</link> <description>New RSS tutorial on <description>New RSS tutorial on W3Schools</description>W3Schools</description> </item></item> <item><item> <title>XML Tutorial</title><title>XML Tutorial</title> <link>http://www.w3schools.com/xml</link><link>http://www.w3schools.com/xml</link> <description>New XML tutorial on <description>New XML tutorial on W3Schools</description>W3Schools</description> </item></item></channel></channel></rss></rss>
Consuming REST Consuming REST with jQuerywith jQuery
How To Make It Work?How To Make It Work?
Consuming REST With Consuming REST With jQueryjQuery
Can be done with three methodsCan be done with three methods $.ajax(…)$.ajax(…)
$.get(…)$.get(…)
$.post(…)$.post(…) $.get(…) $.get(…) and and $.post(…)$.post(…) use use
$.ajax(…)$.ajax(…) under the hood under the hood These methods load data from the These methods load data from the
serverserver Using rest serviceUsing rest service
Return JSON, Atom, RSSReturn JSON, Atom, RSS
Example of $.ajax(…)Example of $.ajax(…)
$.ajax({$.ajax({url: "RestService.svc/students/all",url: "RestService.svc/students/all",type: "GET", type: "GET", timeout: 5000, timeout: 5000, dataType: "json",dataType: "json",success: function (students) {success: function (students) {
$('#loadStudentsButton').html("students loaded");$('#loadStudentsButton').html("students loaded");// do something with the students data// do something with the students data// visualize them, etc…// visualize them, etc…
}} error: function (err) {error: function (err) { $('#loadStudentsButton').html("error: " + err.status);$('#loadStudentsButton').html("error: " + err.status); }}});});
The path of the REST The path of the REST ServiceService
(should be on the same (should be on the same server)server)
Request type ('GET' or Request type ('GET' or 'POST')'POST')
The type of data to The type of data to expect(JSON,XML)expect(JSON,XML)
In case the request is In case the request is successfulsuccessful
In case the request is In case the request is unsuccessfulunsuccessful
Example of Example of jQuery.ajax(…)jQuery.ajax(…) get get requestrequest
Example of $.ajax(…)Example of $.ajax(…)
$.ajax({$.ajax({url: "RestService.svc/students/new",url: "RestService.svc/students/new",type: "POST",type: "POST",timeout: 5000,timeout: 5000,contentType: "application/json",contentType: "application/json",data: JSON.stringify(student),data: JSON.stringify(student),success: function () {success: function () {
$('#createStudentButton').html("student created");$('#createStudentButton').html("student created");//reload the students//reload the students
},},error: function (err) {error: function (err) {
The type of The type of data to sent to data to sent to
the server the server (JSON, XML)(JSON, XML)
We have a student object (in We have a student object (in JSON) and should make it JSON) and should make it readable for the serverreadable for the server
Example of Example of jQuery.ajax(…) jQuery.ajax(…) post post requestrequest
Consuming Our REST Consuming Our REST
with jQuerywith jQueryLive DemoLive Demo
Consuming Twitter Consuming Twitter REST REST
with jQuerywith jQueryLets Make Our Own Twitter?Lets Make Our Own Twitter?
Twitter Rest APITwitter Rest API First lets get familiar with the First lets get familiar with the
Twitter REST APITwitter REST API We are given a set of methods toWe are given a set of methods to
Get a number of tweets for a given Get a number of tweets for a given useruser
Post a tweet using jQueryPost a tweet using jQuery
Search tweetsSearch tweets
Etc…Etc…
First need to register a twitter appFirst need to register a twitter app
Twitter Rest API (2)Twitter Rest API (2)
Registering a Twitter AppRegistering a Twitter App Go to http://dev.twitter.comGo to http://dev.twitter.com Register a AppRegister a App
When your app is registered you When your app is registered you get:get: Consumer keyConsumer key
The key your app authorizes withThe key your app authorizes with
Consumer secretConsumer secret Used for user authenticationUsed for user authentication
Methods of Twitter Methods of Twitter REST APIREST API
GET GET statuses/user_timelinestatuses/user_timeline Returns the 20 most recent statuses Returns the 20 most recent statuses
posted by the authenticating userposted by the authenticating user It is also possible to request another It is also possible to request another
user's timeline by using user's timeline by using The The screen_name screen_name or or user_id user_id parameterparameter
The other users timeline will only be The other users timeline will only be visible if they are not protectedvisible if they are not protected If the authenticating user's follow If the authenticating user's follow