100% found this document useful (11 votes)
1K views28 pages

Ajax Tutorial

Uploaded by

rajesh2kc
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (11 votes)
1K views28 pages

Ajax Tutorial

Uploaded by

rajesh2kc
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Asynchronous JavaScript and XML

(AJaX)

Object Computing, Inc.


Mark Volkmann
mark@[Link]

1 AJaX

Topics Covered
• What is AJaX? • Demo Description
• JavaScript Overview • Demo Sequence Diagrams
• XMLHttpRequest (XHR) • Demo REST Server
• Sarissa JavaScript Library • Demo XHTML
• REST Overview • Demo JavaScript
• Wrapup

2 AJaX

1
What is AJaX?
• A name given to an existing approach
to building dynamic web applications
• Web pages use JavaScript to make asynchronous calls
to web-based services that typically return XML
– allows user to continue interacting with web page
while waiting for data to be returned
– page can be updated without refreshing browser
– results in a better user experience
– there are AJaX libraries that reduce the amount
of JavaScript code that must be written
• Uses a JavaScript class called XMLHttpRequest

3 AJaX

A Good Acronym?
• A is for “asynchronous”
– requests can be made asynchronously or synchronously
– both techniques allow web page to be updated without refreshing it
– anything useful the user can do while processing request?
• if yes then use asynchronous, otherwise use synchronous

• J is for “JavaScript”
– typically JavaScript is used on the client-side (in the browser)
• only programming language supported out-of-the-box by most web browsers
– can use any language on server-side that can
accept HTTP requests and return HTTP responses
• Java servlets, Ruby servlets, CGI scripts, …

• X is for “XML”
– request and response messages can contain XML
• can easily invoke REST-style services
– can really contain any text (single text value, delimited text, …)
4 AJaX

2
Uses For AJaX
• Asynchronous
– examples
• Google Maps – [Link]
– asynchronously loads graphic tiles to support map scrolling
• Google Suggest – [Link]
– asynchronously updates list of possible topic matches
based on what has been typed so far

• Synchronous
– even when there is nothing useful for the user to do
after a request is submitted to a server,
AJaX can be used to retrieve data and
update selected parts of the page
without refreshing the entire page
• better user experience

5 AJaX

JavaScript Overview
• A programming language with syntax similar to Java
• Supported by web browsers
– JavaScript can be downloaded from web servers along with HTML
and executed in the browser
• Syntax to use from HTML these notes use XHTML
instead of HTML
– add <script> tag(s) to head section of HTML
– can embed JavaScript code inside HTML
or refer to external JavaScript files
– embedding
<script type="text/javascript"> ... code ... </script>
– referring
<script type="text/javascript" src="url"></script>

The XHTML DTD declaration for the script tag says <!ELEMENT script (#PCDATA)>,
and the XHTML specs says “Given an empty instance of an element
whose content model is not EMPTY (for example, an empty title or paragraph)
do not use the minimized form (e.g. use <p> </p> and not <p />).

6 AJaX

3
JavaScript Overview (Cont’d)
• JavaScript files cannot include/import others
– HTML must use a script tag to refer to each needed JavaScript file

7 AJaX

XMLHttpRequest
• A JavaScript class supported by most web browsers
• Allows HTTP requests to be sent from JavaScript code
– to send multiple, concurrent requests,
use a different XMLHttpRequest instance for each
• HTTP responses are processed by “handler” functions
– in client-side JavaScript
• Issue
– code to create an XMLHttpRequest object differs between browsers
– can use a JavaScript library such as Sarissa (more detail later)
to hide the differences

8 AJaX

4
XMLHttpRequest Properties
(partial list)

this is a property of
• readyState many JavaScript objects
– 0 = UNINITIALIZED; open not yet called
– 1 = LOADING; send for request not yet called
– 2 = LOADED; send called, headers and status are available
– 3 = INTERACTIVE; downloading response,
responseText only partially set
usually wait for
– 4 = COMPLETED; finished downloading response [Link] == 4

• responseText
– response as text; null if error occurs or ready state < 3
• responseXML
– response as DOM Document object; null if error occurs or ready state < 3
• status – integer status code
• statusText – string status

9 AJaX

XMLHttpRequest Methods
(partial list)

• Basic methods
– open(method, url[, async]) – initializes a new HTTP request
• method can be "GET", "POST", "PUT" or "DELETE"
• url must be an HTTP URL (start with "[Link]
• async is a boolean indicating whether request should be sent asynchronously
– defaults to true
– send(body) – sends HTTP request body can be null
– abort() – called after send() to cancel request
• Header methods
– void setRequestHeader(name, value)
– String getResponseHeader(name)
– String getAllResponseHeaders()
• returns a string where Example return value:
Connection: Keep-Alive
“header: value” pairs Date: Sun, 15 May 2005 [Link] GMT
are delimited by carriage returns Content-Type: text/xml
Server: WEBrick/1.3.1 (Ruby/1.8.2/2004-12-25)
Content-Length: 1810
10 AJaX

5
Sarissa
• An open source JavaScript library that allows the
following to be done in a browser independent way
– create XMLHttpRequest objects ([Link])
– parse XML using DOM (synchronous) or SAX (async.) style
([Link])
– create XML using DOM ([Link])
– transform XML using XSLT (sarissa_ieemu_xslt.js)
– query XML using XPath (sarissa_ieemu_xpath.js)
• Download from [Link]
• Documentation at [Link]

11 AJaX

Using XMLHttpObject
With Sarissa
• To create an XMLHttpRequest
var xhr = new XMLHttpRequest();

• To send synchronous GET request and obtain response


[Link]("GET", url, false); // false for sync
var body = null; // wouldn’t be null for a POST
[Link](body); [Link]
var domDoc = [Link]; gets a string representation
var xmlString = [Link](domDoc); of an DOM node;
mainly used for debugging
• To send asynchronous GET request
[Link]("GET", url, true); // true for async
[Link] = function() { function is called every time
if ([Link] == 4) { readyState value changes;
var domDoc = [Link]; can set onreadystatechange
var xmlString = [Link](domDoc); to the name of a function
defined elsewhere
}
}
var body = null; // wouldn’t be null for a POST
[Link](body);
12 AJaX

6
Using XMLHttpObject
With Sarissa (Cont’d)
• To set a request header
[Link]("name", "value");

• To get a response header


var value = [Link]("name");

13 AJaX

REST Overview
• Stands for REpresentational State Transfer
• Main ideas
– a software component requests a “resource” from a service
• by supplying a resource identifier and a desired media type
– a “representation” of the resource is returned
• a sequence of bytes and metadata to describe it
– metadata is name-value pairs (can use HTTP headers)
– obtaining this representation causes the software component
to “transfer” to a new “state”

14 AJaX

7
REST Overview (Cont’d)
• REST is an architectural style, not a standard or an API
– but can use existing standards including URLs, HTTP and XML
– can be implemented in many ways (such as Java or Ruby servlets)
– used to build distributed applications such as Web apps. and Web services
• Good sources for further reading
– “Building Web Services the REST Way” by Roger L. Costello
• [Link]
– Roy Fielding’s 2000 dissertation (chapter 5)
• [Link]
– RESTwiki - [Link]
– REST mailing list - [Link]

15 AJaX

REST Resources and Identifiers


• What is a REST resource? “Think of RESTful applications
to consist of objects (resources)
– a specific, retrievable thing, not an abstract concept that all have the same API
(PUT, DELETE, GET, POST, etc).
– for example, instead of having a “car” resource For a component of the application
with representations like “photo” and “sales report”, to invoke a method on an object,
those are the resources it issues an HTTP request.”
from a post on the rest-discuss
• car photo from a specific view (front, side and rear) by Jan Algermissen
with JPEG representations
• car sales report for a specific month/year An underlying goal is to
with PDF and XML representations make as many things as
possible retrievable by
• What are good resource identifiers? an HTTP GET request.
[Link] This enables
?make=BMW&model=Z3&year=2001&view=front browser-based testing.
[Link]
[Link]
?make=BMW&model=Z3&year=2001&salesYear=2004&salesMonth=4
[Link]

16 AJaX

8
Demo Description
• Music collection search
– MySQL database is populated off-line from an iTunes XML file
– web page contains
• text field to enter an artist name
– suggests completions like Google Suggest
– database columns include id and name
• list of artists whose name matches what has been typed so far
– update asynchronously during typing
• list of CDs by the selected artist
– updated asynchronously when an artist name is entered or selected
– database columns include id, title and year
• table of track data for selected CD
– updated asynchronously when CD selection changes
– database columns include id, track number, name, time and rating
– requests and responses follow REST style

17 AJaX

Demo Screenshot

track names are bold


if rating >= 4

18 AJaX

9
Demo Pieces
(we’ll focus on boxes with bold text)

CDs iTunes Music Store browser

[Link]
iTunes
[Link]

iTunes Music [Link] [Link]

REST REST
[Link] request response

MySQL [Link]
could have easily written PopulateDB and MusicServer in Java using JDBC/Hibernate and a Servlet

19 AJaX

Getting Artists Whose


Names Begin With prefix
• Request
[Link]

• Response
<artists>
<artist id="141" href="[Link]
Cocteau Twins</artist>
<artist id="72" href="[Link]
Cole, Holly</artist>
<artist id="80" href="[Link]
Cole, Paula</artist>
<artist id="111" href="[Link]
Collins, Phil</artist>
<artist id="48" href="[Link]
Colvin, Shawn</artist>
<artist id="132" href="[Link]
Counting Crows</artist>
<artist id="54" href="[Link]
Cowboy Junkies</artist>
</artists>

20 AJaX

10
Getting Artist Information
• Request
[Link]

• Response
<artist id="97">
<name>Apple, Fiona</name>
<cd artistId="97" id="163">
<title>When The Pawn...</title>
<track rating="3" id="767" cdId="163">On The Bound</track>
<track rating="3" id="768" cdId="163">To Your Love</track>
...
</cd>
- <cd artistId="97" id="164">
<title>Tidal</title>
<track rating="4" id="777" cdId="164">Sleep To Dream</track>
<track rating="4" id="778" cdId="164">Sullen Girl</track>
... Request
</cd> [Link] without “deep”
</artist> Response
<artist id="97">
<name>Apple, Fiona</name>
<cd href="[Link] id="163" />
<cd href="[Link] id="164" />
</artist>

21 AJaX

Getting CD Information
• Request
[Link]

• Response
<cd artistId="97" id="164">
<title>Tidal</title>
<track rating="4" id="777" cdId="164">Sleep To Dream</track>
<track rating="4" id="778" cdId="164">Sullen Girl</track>
...
</cd> Request
[Link] without “deep”
Response
<cd artistId="97" id="164">
<title>Tidal</title>
<track href="[Link] />
<track href="[Link] />
...
</cd>

22 AJaX

11
Getting Track Information
• Request
[Link]

• Response
<track rating="4" id="777" cdId="164">Sleep To Dream</track>

23 AJaX

artistInput onkeydown & onkeyup


Event Handling
WARNING: This is an unusual use of a
sequence diagram where many of the boxes
are JavaScript functions, not objects.

continued on
next diagram

24 AJaX

12
handleArtists Function

25 AJaX

artistSelect and cdSelect


onchange Event Handling

26 AJaX

13
[Link]
• Implemented in Ruby
• Uses WEBrick
– [Link]
– “a Ruby library program to build HTTP servers”
– “a standard library since Ruby-1.8.0”

27 AJaX

[Link] (Cont’d)
#!/usr/bin/ruby

require '../[Link]' # setup for using Active Record to query database


require 'rexml/document'
require 'webrick'

include REXML
include WEBrick

# Add to_s method to REXML Element class.


class Element
def to_s
s = ''; write(s); s
end
end

28 AJaX

14
[Link] (Cont’d)
SERVLET_HOST = 'localhost'
SERVLET_PORT = 2000
SERVLET_NAME = 'music'

class MusicServlet < HTTPServlet::AbstractServlet

# A new servlet instance is created to service each request.


def initialize(server)
super(server)
end

def get_resource_url(type, id)


"[Link]
end

29 AJaX

[Link] (Cont’d)
def do_GET(req, res)
resource_type = req.path_info[1..-1] # remove first character
resource_id = [Link]['id']
starts = [Link]['starts']
@deep = [Link]['deep']

res['Content-Type'] = 'text/xml'
[Link] = case resource_type
when 'artist'
if resource_id and resource_id.size > 0
get_artist(resource_id).to_s
else
get_all_artists(starts).to_s
end invoking to_s method we added
when 'cd' to REXML Element class
get_cd(resource_id).to_s
when 'track'
get_track(resource_id).to_s
else
"unsupported resource type #{resource_type}"
end
end

30 AJaX

15
[Link] (Cont’d)
def get_all_artists(starts)
artists_element = [Link]('artists')

artists = Artist.starts_with(starts)

[Link] do |artist|
artist_element = [Link]('artist', artists_element)
artist_element.add_attribute('id', [Link])
artist_element.add_attribute(
'href', get_resource_url('artist', [Link]))
artist_element.add_text([Link])
end

artists_element
end

31 AJaX

[Link] (Cont’d)
def get_artist(artist_id)
artist = [Link](artist_id)
return "no artist with id #{artist_id} found" if artist == nil

artist_element = [Link]('artist')
artist_element.add_attribute('id', artist_id)
name_element = [Link]('name', artist_element)
name_element.add_text([Link])

[Link] do |cd|
cd_element = if @deep
artist_element.add_element(get_cd([Link]))
else
[Link]('cd', artist_element)
end
cd_element.add_attribute('id', [Link])
cd_element.add_attribute('href', get_resource_url('cd', [Link])) if not @deep
end

artist_element
end

32 AJaX

16
[Link] (Cont’d)
def get_cd(cd_id)
cd = [Link](cd_id)
return "no cd with id #{cd_id} found" if cd == nil

cd_element = [Link]('cd')
cd_element.add_attribute('id', [Link])
cd_element.add_attribute('artistId', cd.artist_id)
title_element = [Link]('title', cd_element)
title_element.add_text([Link])

[Link] do |track|
track_element = if @deep
cd_element.add_element(get_track([Link]))
else
[Link]('track', cd_element)
end
track_element.add_attribute('href',
get_resource_url('track', [Link])) if not @deep
end

cd_element
end

33 AJaX

[Link] (Cont’d)
def get_track(track_id)
track = [Link](track_id)
return "no track with id #{track_id} found" if track == nil

track_element = [Link]('track')
track_element.add_attribute('id', [Link])
track_element.add_attribute('cd_id', track.cd_id)
track_element.add_attribute('rating', [Link])
track_element.add_text([Link])

track_element
end

end # class MusicServlet

34 AJaX

17
[Link] (Cont’d)
# Create WEBrick server.
# Configure so files in DocumentRoot can be accessed
# with the URL [Link]
config = {
:DocumentRoot => '/AJaX/MusicCollection/web',
:FancyIndexing => true, # If URI refers to a directory, list the contents.
:Port => SERVLET_PORT
}
server = [Link](config)

# Add mime type for XHTML.


mimeTypes = [Link][:MimeTypes]
mimeTypes['xhtml'] = 'text/html'

# Allow the server to be stopped with Ctrl-c.


trap('INT') { [Link] }
trap('TERM') { [Link] }

[Link]("/#{SERVLET_NAME}", MusicServlet)
[Link]

35 AJaX

[Link]
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"[Link]

<html xmlns="[Link]
<head>
<title>Music Collection</title>

<link rel="stylesheet" type="text/css" href="[Link]" />

<script type="text/javascript" src="[Link]"></script>


<script type="text/javascript" src="sarissa_ieemu_xpath.js"></script>
<script type="text/javascript" src="[Link]"></script>
<script type="text/javascript" src="[Link]"></script>
<script type="text/javascript" src="[Link]"></script>
</head>
<body>
<h1>Music Collection</h1>

36 AJaX

18
[Link] (Cont’d)
<form id="myForm" action="">
<table>
<tr>
<th id="artistHeader">Artist</th>
<th id="cdHeader">CDs</th>
<th id="trackHeader">Tracks</th>
</tr>
<tr>
<td valign="top">
<input type="text" id="artistInput" tabindex="1"
onkeydown="artistKeydown(event, this)"
onkeyup="artistKeyup(event, this)" />
</td>
<td valign="top" rowspan="2">
<select id="cdSelect" tabindex="3" size="12"
onchange="cdSelected(this)">
<option></option> <!-- XHTML requires at least one option -->
</select>
</td>

37 AJaX

[Link] (Cont’d)
<td valign="top" rowspan="2">
<table id="trackTable">
<tr>
<th id="trackNumber">#</th>
<th id="trackName">Name</th>
<th id="trackRating">Rating</th>
</tr>
</table>
</td>
</tr>
<tr>
<td id="artistSelectTD">
<select id="artistSelect" tabindex="2" size="10"
onchange="artistSelected(this)">
<option></option> <!-- XHTML requires at least one option -->
</select>
</td>
</tr>
</table>

38 AJaX

19
[Link] (Cont’d)
<!-- for debugging -->
<!--p><textarea id="log" rows="20" cols="80"></textarea></p-->

<p><input type="reset" /></p>


</form>
</body>
</html>

39 AJaX

[Link]
// This contains utility functions make working with DHTML easier.

// Adds an option to the end of a select.


function addOption(select, option) {
if (isIE()) {
[Link](option);
} else {
[Link](option, null);
}
}

// Removes all the options from a given select component.


function clearSelect(select) {
while ([Link] > 0) {
[Link](0);
}
}

40 AJaX

20
[Link] (Cont’d)
// Delete all the rows in a given table except the header row.
function clearTable(table) {
rowCount = [Link];
for (i = rowCount - 1; i > 0; i--) {
[Link](i);
}
}

// Gets the text inside a given DOM element.


// TODO: This should really concatenate the values
// of all text nodes inside the element.
function getText(element) {
return [Link];
}

41 AJaX

[Link] (Cont’d)
// Highlights the characters at the end of an input field
// starting from a given position.
function highlightInput(input, start) {
totalLength = [Link];
if (isIE()) {
range = [Link]();
[Link]("character", start);
[Link]();
} else {
[Link](start, [Link]);
}
}

// Determines if the web browser is IE.


function isIE() {
var browserName = [Link];
return browserName == "Microsoft Internet Explorer";
}

42 AJaX

21
[Link] (Cont’d)
// Logs a message to a text area with an id of "log"
// for debugging purposes.
function log(message) {
[Link]("log").value += message + "\n";
}

// Sends an asynchronous HTTP request to a given URL


// whose response will be sent to a given handler.
function send(url, handler) {
// XMLHttpRequest is used to send asynchronous HTTP requests.
// Firefox seems to require creating a new XMLHttpRequest object
// for each request.
xhr = new XMLHttpRequest(); // from Sarissa

[Link] = handler;
async = true;
This is the main place where
[Link]("GET", url, async); AJaX appears in this application!
body = null;
[Link](body); Don’t blink or you’ll miss it!
return xhr;
}

43 AJaX

[Link]
// Keycodes used by event handling functions.
var backspaceKeycode = 8;
var ctrlKeycode = 17;
var downArrowKeycode = 40;
var shiftKeycode = 16;

// Base URL of asynchronous HTTP requests.


var baseURL = "[Link]

// Keeps track of whether the Ctrl key is currently down.


var ctrlKeyDown = false;

// The characters of the artist name that the user typed.


var lastArtistPrefix = "";

// Holds an XMLHttpRequest object that is used to


// send asynchronous HTTP requests.
var xhr = null;

44 AJaX

22
[Link] (Cont’d)
// Handles keydown events in the artist input field.
function artistKeydown(event, component) {
if ([Link] == ctrlKeycode) ctrlKeyDown = true;
if ([Link] == downArrowKeycode) {
// Move focus from artistInput to artistSelect.
[Link]("artistSelect").focus();
}
}

// Handles keyup events in the artist input field.


function artistKeyup(event, component) {
// For example, the user may have pressed Ctrl-P to print.
// At this point ctrlKeyDown could be true and
// [Link] could be the code for 'P'.
if (!ctrlKeyDown) getArtists(event, component);
if ([Link] == ctrlKeycode) ctrlKeyDown = false;
}

45 AJaX

[Link] (Cont’d)
// Handles selections of artists in the artist select component.
function artistSelected(component) {
index = [Link];
value = [Link][index].text;

// Copy selected value to text input field.


[Link]("artistInput").value = value;

getCDs(); // asynchronously
}

// Handles selections of CDs in the CD select component.


function cdSelected(component) {
index = [Link];
cdId = [Link][index].value;
getTracks(cdId); // asynchronously
}

46 AJaX

23
[Link] (Cont’d)
// Sends an asynchronous request to obtain
// a list of artists whose name begins with
// the prefix entered in a text input component.
function getArtists(event, component) {
if ([Link] == shiftKeycode) return;

if ([Link] == backspaceKeycode) {
artistPrefix = [Link]
(0, [Link] - 1);
} else {
artistPrefix = ltrim([Link]); // in [Link]
}
lastArtistPrefix = artistPrefix

if ([Link] == 0) {
[Link] = "";
clearSelect([Link]("artistSelect"));
clearSelect([Link]("cdSelect"));
clearTable([Link]("trackTable"));
} else {
url = baseURL + "artist?starts=" + artistPrefix;
xhr = send(url, handleArtists);
}
}

47 AJaX

[Link] (Cont’d)
// Sends an asynchronous request to obtain
// a list of CDs by the artist selected in a select component.
function getCDs() {
select = [Link]("artistSelect");
index = [Link];
option = [Link][index];
artistId = [Link]
url = baseURL + "artist?id=" + artistId + "&deep";
xhr = send(url, handleCDs);
}

// Sends an asynchronous request to obtain


// a list of tracks on a CD selected in a select component.
function getTracks(cdId) {
url = baseURL + "cd?id=" + cdId + "&deep";
xhr = send(url, handleTracks);
}

48 AJaX

24
[Link] (Cont’d)
// Handles the response from asynchronous requests
// for information about artists
// whose name begins with a given prefix.
function handleArtists() {
if ([Link] == 4) {
doc = [Link];
//log("handleArtists: xml = " + [Link](doc));
if ([Link] == null) {
alert("Is the server running?");
return;
}

[Link]("SelectionLanguage", "XPath");
nodes = [Link]("/artists/artist"); // from Sarissa

artistSelect = [Link]("artistSelect");
clearSelect(artistSelect);

if ([Link] == 0) return;

49 AJaX

[Link] (Cont’d)
// Add an option to artistSelect for each matching artist.
for (i = 0; i < [Link]; i++) {
artist = nodes[i];
name = getText(artist); default selected
id = [Link]('id')
option = new Option(name, id, false, i == 0);
addOption(artistSelect, option);
} selected

// Set artist text field to first choice.


input = [Link]("artistInput");
firstArtistName = getText(nodes[0]);
[Link] = firstArtistName;

// Highlight suffix supplied by search.


highlightInput(input, [Link]);

getCDs();
}
}

50 AJaX

25
[Link] (Cont’d)
// Handles the response from asynchronous requests
// for information about CDs by an artist.
function handleCDs() {
if ([Link] == 4) {
doc = [Link];
//log("handleCDs: xml = " + [Link](doc));

[Link]("SelectionLanguage", "XPath");
nodes = [Link]("/artist/cd"); // from Sarissa

select = [Link]("cdSelect");
clearSelect(select);

51 AJaX

[Link] (Cont’d)
firstId = 0;

// Add an option to cdSelect for each CD.


for (i = 0; i < [Link]; i++) {
cd = nodes[i];
title = getText([Link]("title")); // from Sarissa
id = [Link]('id');
if (i == 0) firstId = id;
option = new Option(title, id, false, i == 0);
addOption(select, option);
}

getTracks(firstId);
}
}

52 AJaX

26
[Link] (Cont’d)
// Handles the response from asynchronous requests
// for information about tracks on a CD.
function handleTracks() {
if ([Link] == 4) {
doc = [Link];
//log("handleTracks: xml = " + [Link](doc));

[Link]("SelectionLanguage", "XPath");
nodes = [Link]("/cd/track"); // from Sarissa

table = [Link]("trackTable");

// Delete all the table rows except the header row.


rowCount = [Link];
for (i = rowCount - 1; i > 0; i--) {
[Link](i);
}

53 AJaX

[Link] (Cont’d)
// Add a row to trackTable for each track.
for (i = 0; i < [Link]; i++) {
track = nodes[i];
name = getText(track);
id = [Link]('id');
rating = [Link]('rating');

row = [Link](i + 1);


[Link] = "white";

cell = [Link](0); // track number


[Link] = "right"
[Link] = i + 1;

cell = [Link](1); // track name


[Link] = name;
if (rating >= 4) [Link] = "favorite";

cell = [Link](2); // track rating


[Link] = "center"
[Link] = rating;
}
}
}

54 AJaX

27
Wrap Up
• Summary
– don’t have to refresh the browser page
in order to display new data from the server
– get data asynchronously with XMLHttpRequest
• ToDos
– don’t send request for artists that match the name typed
until some amount of time (1 second?) has passed
without more characters being typed
– test performance with REST server and web server
running on different machines than browser
– could improve performance by caching REST responses
in client-side JavaScript
• what caching is supplied automatically by the browser?
– display years after CDs
– add sequence numbers to request and response messages
so they are paired correctly when there are concurrent requests?

55 AJaX

Wrap Up (Cont’d)
• Any questions?
• Thank you very much for attending!

56 AJaX

28

You might also like