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].
XMLHttpRequest objectXMLHttpRequest object requests info from serverXMLHttpRequest fires an event when data arrives
new Ajax.Request("url", {
option : value,
option : value,
...
option : value
});
Ajax.Request object to request a page from a server using Ajax{} braces
(an anonymous JavaScript object)
XMLHttpRequest; works well in all browsers| 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)
|
|
new Ajax.Request("foo/bar/mydata.txt", {
method: "get",
onSuccess: myAjaxSuccessFunction
});
...
function myAjaxSuccessFunction(ajax) {
do something with ajax.responseText;
}
onSuccess eventajax| 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);
}
responseText, to access the fetched text content
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;
}
}
new Ajax.Request("lookup_account.php", {
method: "get",
parameters: {name: "Ed Smith", age: 29, password: "abcdef"},
onFailure: ajaxFailure,
onException: ajaxFailure
});
...
"?" + ...
parameters object, {} braces with name : value pairs
"name=Ed+Smith&age=29&password=abcdef") POST request
new Ajax.Request("url", {
method: "post",
parameters: {name: value, name: value, ..., name: value},
onSuccess: functionName,
onFailure: functionName,
onException: functionName
});
method should be changed to "post" (or omitted; post is default)XMLHttpRequest security restrictions
http://www.foo.com/a/b/c.html can only fetch from www.foo.com
new Ajax.Updater("id", "url", {
method: "get"
});
Ajax.Updater fetches a file and injects its content into an element as innerHTML
Ajax.Request, but Ajax.Updater saves you some typing and workid of element to inject intoonSuccess handler not needed (but onFailure, onException handlers may still be useful)
new Ajax.Updater({success: "id", failure: "id"}, "url", {
method: "get",
insertion: "top"
});
success and/or failure id
success element will be filled if the request succeedsfailure element (if provided) will be filled if the request failsinsertion parameter specifies where in the element to insert the text (top, bottom, before, after)
new Ajax.PeriodicalUpdater("id", "url", {
frequency: seconds,
name: value, ...
});
Ajax.PeriodicalUpdater repeatedly fetches a file at a given interval and injects its content into an element as innerHTMLonSuccess handler not needed (but onFailure, onException handlers may still be useful)Ajax.Updater can be passed
Ajax.Responders.register({
onEvent: functionName,
onEvent: functionName,
...
});