46
AJAX Dening principles of AJAX AJAX in action:Crane 1. Browser hosts an application, not content 2. Server delivers data not content 3. User interaction with the application can be uid and continuous 4. This is real coding Tuesday, October 22, 13

AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

  • Upload
    others

  • View
    16

  • Download
    1

Embed Size (px)

Citation preview

Page 1: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

AJAX

• Defining principles of AJAX

AJAX in action:Crane

1.Browser hosts an application, not content

2.Server delivers data not content

3.User interaction with the application can be fluid and continuous

4.This is real coding

Tuesday, October 22, 13

Page 2: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

AJAX

1.Browser hosts an application, not content

AJAX in action:Crane

• Static Web model

• every page is new content

• Real-time Web model

• download a program at first

• every page is new data

• Some server functionality is moved to browser

• example, the shopping basket is in the client

vs

Tuesday, October 22, 13

Page 3: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

AJAX AJAX in action:Crane

1.Browser hosts an application, not content

HTML

HTML

HTML

HTML

Tuesday, October 22, 13

Page 4: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

AJAX

• Browser hosts an application, not content

AJAX in action:Crane

HTMLJavascript

XML

XML

XML

XML

XML

HTML

Tuesday, October 22, 13

Page 5: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

AJAX

2.Server delivers data not content

AJAX in action:Crane

Web 1.0

Tuesday, October 22, 13

Page 6: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

AJAX

2.Server delivers data not content

AJAX in action:Crane

Web 2.0

Tuesday, October 22, 13

Page 7: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

AJAX

2.Server delivers data not content

AJAX in action:Crane

Tuesday, October 22, 13

Page 8: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

AJAX

3.User interaction with the application can be fluid and continuous

AJAX in action:Crane

• Typically when a page is submitting data, the user is in limbo

• Use the shopping cart example

•Google Suggest

• Sovereign versus Transient Applications

Tuesday, October 22, 13

Page 9: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

AJAX

4.This is real coding

AJAX in action:Crane

• jQuery (http://jquery.com)

• Angularjs (http://angularjs.org/)

• Backbonejs (http://backbonejs.org/)

• emberjs (http://emberjs.com)

• Prototype (http://www.prototypejs.org/)

• ExtJS (http://www.extjs.com/)

• very good for prebuilt themes and controls, but not very customizable

• YUI (http://developer.yahoo.com/yui/)

•MooTools (http://mootools.net/) - very compact, much smaller than the others

• Dojo (http://dojotoolkit.org/)

Tuesday, October 22, 13

Page 11: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Tuesday, October 22, 13

Page 12: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

User Interaction:Making AJAX

Assoc. Professor Donald J. PattersonINF 133 Fall 2013

12Tuesday, October 22, 13

Page 13: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

jQuery Step10.html

Tuesday, October 22, 13

Page 14: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

jQuery

Tuesday, October 22, 13

Page 15: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

jQuery

Tuesday, October 22, 13

Page 16: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Task 14

• Two problems:

• Javascript does not allow you to make an XMLHttpRequest from an external domain

• XMLHttpRequest is the javascript function which retrieves data asynchronously

• Not just XML, but any data

• For security reasons it was restricted

• It is used by the jQuery .ajax() function

• An http URL cannot request from an https URL and vice versa

The weeds in the weeds

What is this?

Tuesday, October 22, 13

Page 17: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Task 14

http://mydomain.com/index.html http://mydomain.com/data.xmlXMLHttpRequest

http://mydomain.com/index.html http://mydomain.com/data.jsonXMLHttpRequest

http://mydomain.com/index.html http://yourdomain.com/data.xmlXMLHttpRequest

http://mydomain.com/index.html http://yourdomain.com/data.jsonXMLHttpRequest

The weeds in the weeds

Tuesday, October 22, 13

Page 18: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Task 14

• Solutions to the cross-domain security restriction

• Run a “proxy”

• a php proxy

• You request

• http://mydomain.com/proxy.php?http://yourdomain.com/data.xml

• Javascript thinks its coming from local server

• proxy.php contains something like this code:

• php doesn’t run on ics.uci.edu

• You need to use “students.ics.uci.edu”

• http://www.ics.uci.edu/computing/web/faqs.php#students

Tuesday, October 22, 13

Page 19: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Task 14

• This is the contents of a file on your server

• It is running a language called php

• the language is interpreted by your web server code

• server-side scripting

Browser proxy Data

Requests data viaAJAX call

Forwards request to foreign domain

Tuesday, October 22, 13

Page 20: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

• Solutions to the cross-domain security restriction

• Use JSONP

• Requires server support

• Yahoo APIs support this

• Leverages Javascript loophole

• XMLHttpRequests are restricted

• Remote Javascript is not

Task 14

Tuesday, October 22, 13

Page 21: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

• JSONP adds a <script src=> element to your web page with the external URL as the src target

•Getting JSON looks like this:

• Request: http://yourdomain.com/data.json

• Return: {“hello”:”world”}

•Getting JSONP looks like this:

• Request http://yourdomain.com/data.jsonp?callback=myCallback

• Return: myCallback(“{\”hello\”:\”world\”}”)

• Your webpage writes a function called myCallback to deal with the data

Task 14

Tuesday, October 22, 13

Page 22: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

• Security issue

• You are running server generated code on your machine

• !

Task 14

Tuesday, October 22, 13

Page 23: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Browser Data

Requests javascript viaAJAX call

Task 14

Proxy

My Javascript

$ajax(url, callback)

function callback(data){parse datainsert into HTML table

}

My HTML<head>

jquery scriptmy javascript

</head>

<body><table>

Here be a table!</table>

</body>

GeoDataurl

data(JSON/XML)

FOR ASSIGNMENT 2...

Tuesday, October 22, 13

Page 24: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

jQuery

Tuesday, October 22, 13

Page 25: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

jQuery

Tuesday, October 22, 13

Page 26: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

jQuery

Tuesday, October 22, 13

Page 27: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

jQuery

•What if you have something other than JSON to load?

• XML

• jQuery.parseXML()

•Where can I find info about jQuery actions?

• jQuery API Reference

• for example, html() vs text() vs append()?

Tuesday, October 22, 13

Page 28: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

jQuery - Final example Step14

Tuesday, October 22, 13

Page 29: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

jQuery

Tuesday, October 22, 13

Page 30: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

jQuery - XML

Tuesday, October 22, 13

Page 31: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

jQuery - JSON

Tuesday, October 22, 13

Page 32: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

jQuery - JSONP

Tuesday, October 22, 13

Page 33: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

jQuery

• Task 14

•Geocoded Feeds

• http://api.flickr.com/services/feeds/geo/QDd_2PObCZ4ZsRM6Sw&format=json

• Use dynamic data, not static data

• jQuery

• HTML Table

• AJAX request

Tuesday, October 22, 13

Page 34: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Task 14 strategy

• The goal is a web page

• which has issued an AJAX request

• parsed the data

• and displayed it in a new way

Tuesday, October 22, 13

Page 35: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Task 14 strategy

Get a "Hello World" web

page running

Find some geo-data in XML or JSON format

http://api.flickr.com/services/feeds/geo/United+States/California/Los+Angeles&format=json

http://www.ics.uci.edu/~djp3/classes/2010_09_INF133/Assignments/Assignment02/Step1.html

Pick a library to use for

development

jQuery?

Pick a way to avoid cross-

domain security

php Proxy?JSONP?Yahoo Pipes?

Execute an AJAX request

from within your webpage

Parse and insert the data into

your web page

Tuesday, October 22, 13

Page 36: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Task 14 strategy

Get a "Hello World" web

page running

Find some geo-data in XML or JSON format

http://api.flickr.com/services/feeds/geo/United+States/California/Los+Angeles&format=json

http://www.ics.uci.edu/~djp3/classes/2010_09_INF133/Assignments/Assignment02/Step1.html

Pick a library to use for

development

jQuery?

Pick a way to avoid cross-

domain security

php Proxy?JSONP?Yahoo Pipes?

Execute an AJAX request

from within your webpage

Parse and insert the data into

your web page

Tuesday, October 22, 13

Page 37: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Task 14 strategy

Get a "Hello World" web

page running

Find some geo-data in XML or JSON format

http://api.flickr.com/services/feeds/geo/United+States/California/Los+Angeles&format=json

http://www.ics.uci.edu/~djp3/classes/2010_09_INF133/Assignments/Assignment02/Step1.html

Pick a library to use for

development

jQuery?

Pick a way to avoid cross-

domain security

php Proxy?JSONP?Yahoo Pipes?

Execute an AJAX request

from within your webpage

Parse and insert the data into

your web page

Tuesday, October 22, 13

Page 38: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Task 14 strategy

Get a "Hello World" web

page running

Find some geo-data in XML or JSON format

http://api.flickr.com/services/feeds/geo/United+States/California/Los+Angeles&format=json

http://www.ics.uci.edu/~djp3/classes/2010_09_INF133/Assignments/Assignment02/Step1.html

Pick a library to use for

development

jQuery?

Pick a way to avoid cross-

domain security

php Proxy?JSONP?Yahoo Pipes?

Execute an AJAX request

from within your webpage

Parse and insert the data into

your web page

Tuesday, October 22, 13

Page 39: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Task 14 strategy

Get a "Hello World" web

page running

Find some geo-data in XML or JSON format

http://api.flickr.com/services/feeds/geo/United+States/California/Los+Angeles&format=json

http://www.ics.uci.edu/~djp3/classes/2010_09_INF133/Assignments/Assignment02/Step1.html

Pick a library to use for

development

jQuery?

Pick a way to avoid cross-

domain security

php Proxy?JSONP?Yahoo Pipes?

Execute an AJAX request

from within your webpage

Parse and insert the data into

your web page

Tuesday, October 22, 13

Page 40: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Task 14 strategy

Get a "Hello World" web

page running

Find some geo-data in XML or JSON format

http://api.flickr.com/services/feeds/geo/United+States/California/Los+Angeles&format=json

http://www.ics.uci.edu/~djp3/classes/2010_09_INF133/Assignments/Assignment02/Step1.html

Pick a library to use for

development

jQuery?

Pick a way to avoid cross-

domain security

php Proxy?JSONP?Yahoo Pipes?

Execute an AJAX request

from within your webpage

Parse and insert the data into

your web page

Tuesday, October 22, 13

Page 41: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Task 14 strategy

• Find some geo-data in XML or JSON format

• (Hint: RSS is a specific type of XML)

•What is geo-data?

• Anything that relates data to a spot on the earth

• Data with a latitude and longitude

• Data with an address

• Data with a zip code

• Data with a county

• For example:

• Photos taken in Los Angeles

• http://www.flickr.com/places/United+States/California/Los+Angeles

Tuesday, October 22, 13

Page 42: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Browser Data

Requests javascript viaAJAX call

Task 14

Proxy

My Javascript

$ajax(url, callback)

function callback(data){parse datainsert into HTML table

}

My HTML<head>

jquery scriptmy javascript

</head>

<body><table>

Here be a table!</table>

</body>

GeoDataurl

data(JSON/XML)

FOR ASSIGNMENT 2...

Tuesday, October 22, 13

Page 43: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Task 14

• At a high-level requesting data asynchronously requires:

• The location of where you want to get the data from

• (for us these are the 5 geo-feeds)

•What function to call when the data is ready

• Because we aren’t waiting

• aka, “the call back function”

• The native function call to do this is called

• “XMLHttpRequest”

Tuesday, October 22, 13

Page 44: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

jQuery

• Task 14

• Present the data as a table

• http://www.w3schools.com/html/html_tables.asp

• HTML tables overview

• 3 primary tags

• <table>

• <tr>

• <td>

Tuesday, October 22, 13

Page 45: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

jQuery

• Basic table

• <table>

• <tr>

• <td>

Tuesday, October 22, 13

Page 46: AJAX AJAX in action:Crane ning principles of AJAXdjp3/classes/2013_09_INF133/Lectures/Lecture_0… · AJAX 1.Browser hosts an application, not content AJAX in action:Crane •Static

Tuesday, October 22, 13