Professional Documents
Culture Documents
http://labs.google.com/suggest
Lecture 17: Ajax
(Asynchronous
JavaScript And XML)
Wendy Liu
CSC309F Fall 2007
1 2
Low-level View
The Basics
3 4
1
Classic vs. Ajax Synchronous vs. Asynchronous
5 6
2
XMLHttpRequest Properties onreadystatechange
Receiving data (handle response) Defines a function to receive data returned by
onreadystatechange
the server after a request is sent
readyState Must be set before sending request
responseXML
purpose (with an empty body for now)
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
// code for receiving response data
}
9 10
3
responseText responseXML
Retrieve
text data returned by the server Retrieve document data returned by the server
Type: DOMString (readonly) Type: Document (readonly)
xmlhttp.onreadystatechange=function()
{ var xmldoc=xmlhttp.responseXML.documentElement;
if (xmlhttp.readyState==4)
{ You can access it as a DOM document
document.getElementById(formentry).value = As you did in A1
xmlhttp.responseText;
}
}
13 14
4
The Basic Ajax Process
JavaScript
Define an object for sending HTTP requests
Initiate request
Get request object
Examples Designate a request handler function
Supply as onreadystatechange attribute of request
Initiate
a GET or POST request
Putting it Together Send data
Handle response
Waitfor readyState of 4 and HTTP status of 200
Extractreturn text with responseText or responseXML
Do something with result
17 18
19 20
5
Initiate Request Handle Response
function sendRequest() { function handleResponse() {
request = getRequestObject(); Response handler if (request.readyState == 4) {
function name
request.onreadystatechange =
alert(request.responseText);
handleResponse;
request.open("GET", "message-data.html", }
true); }
request.send(null);
} URL of server-side
resource Text of server Response is returned from server
response (handler is invoked multiple times)
21 22
6
XHTML Code (message-data.html) The Basic Process: Results
Some random message
25 26
Cross-Browser XMLHttpRequest
function getRequestObj() {
var xmlHttp;
try { // Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
} catch (e) { // Internet Explorer
try { High-level View
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) { Frameworks
alert("Your browser does not support AJAX!");
return null;
}
}
} return xmlHttp;
}
27 28
7
Motivation Client-Side Frameworks I
Challenges of Ajax Rico
Browser compatibilities Designed for drag-and-drop actions, data grids,
Repeated coding effort and what they term cinematic effects (moving
widgets, fading a div, and so on)
http://openrico.org
scriptaculous
Animation framework, drag and drop, Ajax
controls, DOM utilities, and unit testing
http://script.aculo.us/
29 30
31 32
8
Simple Wrappers for XMLHttpRequest Client-Side Toolkits
SACK (Simple Ajax Code Kit) Yahoo User Interface Library (YUI)
Free JavaScript toolkit with some Ajax support
http://twilightuniverse.com/projects/sack/
http://developer.yahoo.com/yui/
XHConn Google
http://xkr.us/code/javascript/XHConn Google Ajax API
Search, Feed, Map API
http://code.google.com/apis/ajax/
Google Mapplet
http://code.google.com/apis/maps/documentation/mapplets
Google Web Toolkit
Write code in Java, translate it to JavaScript
http://code.google.com/webtoolkit/
33 34
35 36
9
Server-side Framework III
Ruby on Rails
Allow rapid development of Web-based
applications
Rails has good support for Ajax with several
Correction: REST
built-in JavaScript libraries that wrap many
common features
The original slide is correct
http://www.rubyonrails.org
37 38
Actions (Verbs)
Controllers cannot have arbitrary verbs, only
these:
POST
Create - create a new resource
GET
Retrieve - retrieve a representation of a resource
PUT
Update - update a resource
DELETE
Delete - delete a resource
39
10