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].
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.
XMLHttpRequest
XMLHttpRequest
object that can fetch files from a web server
XMLHttpRequest
objectXMLHttpRequest
object requests info from serverXMLHttpRequest
fires an event when data arrives
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());
this
new Ajax.Request("url", { option : value, option : value, ... option : value });
Ajax.Request
object to request a page from a server using Ajax{}
braces
XMLHttpRequest
; works well in all browsersoption | 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 contentnew 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
requestnew 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)