Java Reference
In-Depth Information
options that the object uses when making a request. The options object can contain a variety of option
properties to alter the behavior of Ajax.Request(); the following table describes just a few of them.
Option
Description
asynchronous
Determines whether the
XMLHttpRequest
object makes the request in asyn-
chronous mode or not. The default is
true
.
method
The HTTP method used for the request. The default is
“post”
.
“get”
is
another valid value.
onSuccess
A callback function invoked when the request completes successfully.
onFailure
A callback function invoked when the request completes, but results in an
error status code.
parameters
Either a string containing the parameters to send with the request, or an
object containing the parameters and their values.
For a complete list of options, visit the Prototype documentation at
http://www.prototypejs.org/
api/ajax/options
.
All callback functions are executed and passed a parameter containing the XMLHttpRequest object
used to make the request. Making a request with Prototype looks something like the following code:
function request_onsuccess(request)
{
alert(request.responseText);
}
function request_onfailure(request)
{
alert(“An error occurred! HTTP status code is “ + request.status);
}
var options = new Object();
options.method = “get”;
options.onSuccess = request_onsuccess;
options.onFailure = request_onfailure;
new Ajax.Request(“someTextFile.txt”, options);
The fi rst few lines of code defi ne the request_onsuccess() and request_onfailure() functions.
These functions all accept one parameter called request. The value of this parameter will be the
XMLHttpRequest object used to make the request.
Prototype actually passes an
Ajax.Response
object to all Ajax request callbacks. It is very similar
to the
XMLHttpRequest
object and adds extra functionality. To access the
XMLHttpRequest
object
directly, use the
transport
property; for example,
request.transport
. After the function defi ni-
tion, you create an
options
object The fi rst option you set is the method option, which you set to
get
.
The next option is the
onSuccess
option, and you assign the
request_onsuccess()
function to
this option. The fi nal option is
onFailure
, which you assign the
request_onfailure()
function.