CSSE 290 Web Programming

Lecture 20: Ajax

Reading: 12.1 - 12.2

Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.

Otherwise note: Claude Anderson was given permission to modify the slides for CSSE 290 at Rose-Hulman by author Jessica Miller. The authors' original slides, based on Web Programming Step by Step, can be seen at http://webstepbook.com.

Some of the examples in some days' slides are from David Fisher at Rose-Hulman, who was kind enough to allow me to use them. My intention is to mark these examples with [DSF].

Valid HTML Valid CSS!

Announcements

Important note: I sent you two files yesterday. Change their names to garden.ws and garden2.ws, and copy (SFTP) them into the puzzles folder in your HW4 submission on wwwuser.csse.rose-hulman.edu, if you have not already done so.

Synchronous web communication

synchronous communication

Web applications and Ajax

Ajax bleach

Asynchronous web communication

synchronous communication

XMLHttpRequest

A typical Ajax request

request
  1. user clicks, invoking an event handler
  2. handler's code creates an XMLHttpRequest object
  3. XMLHttpRequest object requests info from server
  4. server retrieves appropriate data, sends it back
  5. XMLHttpRequest fires an event when data arrives
    • we attach a handler function to this event (callback)
  6. callback event handler processes the data and displays it

An aside: Creating a new anonymous object

var name = {
	fieldName: value,
	...
	fieldName: value
};
var p = {
	x: 4,
	y: 3,
	distanceFromOrigin: function() {
		return Math.sqrt(this.x * this.x + this.y * this.y);
	}
};
alert(p.x + ", " + p.y + ", dist=" + p.distanceFromOrigin());

Prototype's Ajax model

new Ajax.Request("url", {
	option : value,
	option : value,
	...
	option : value
});

Prototype Ajax options

option description
method how to fetch the request from the server (default "post")
asynchronous should request be sent asynchronously in the background? (default true, for good reasons)
parameters query parameters to pass to the server, if any (as a string or object)
onSuccess event: request completed successfully
onFailure event: request was unsuccessful
onException event: request has a syntax error, security error, etc.
others: contentType, encoding, requestHeaders; events: onCreate, onComplete, on### (for HTTP error code ###, example on404)

Prototype Ajax example

	new Ajax.Request("foo/bar/mydata.txt", {
		method: "get",
		onSuccess: myAjaxSuccessFunction
	});
	...

function myAjaxSuccessFunction(ajax) {
	do something with ajax.responseText;
}

Ajax response object's properties

property description
status the request's HTTP result code (200 = OK, etc.)
statusText HTTP status code text
responseText the entire text of the fetched file, as a string
responseXML,
responseJSON
the entire contents of the fetched file, in other formats (seen later)
function myAjaxSuccessFunction(ajax) {
	alert(ajax.responseText);
}

Handling Ajax errors

	new Ajax.Request("url", {
		method: "get",
		onSuccess: functionName,
		onFailure: ajaxFailure,
		onException: ajaxFailure
	});
	...
function ajaxFailure(ajax, exception) {
  alert("Error making Ajax request:" + 
	"\n\nServer status:\n" + ajax.status + 
    " " + ajax.statusText + 
	"\n\nServer response text:\n" + ajax.responseText);
  if (exception) {
	throw exception;
	}
}

Passing query parameters to a request

new Ajax.Request("lookup_account.php", {
	method: "get",
	parameters: {name: "Ed Smith", age: 29, password: "abcdef"},
	onFailure: ajaxFailure,
	onException: ajaxFailure
});
...

Creating a POSTrequest

new Ajax.Request("url", {
	method: "post",
	parameters: {name: value, name: value, ..., name: value},
	onSuccess: functionName,
	onFailure: functionName,
	onException: functionName
});