Java Reference
In-Depth Information
The first few lines of code define the
requestSuccess()
and
requestFailed()
functions. These
functions accept a parameter called
transport
—a special object that contains the server's response
(more on this later).
After the function definitions, you create an
options
object that contains properties for the
HTTP
method
option, the
onSuccess
option, and the
onFailure
option. Then, you finally make
the request for the
someTextFile.txt
file, passing the
options
object to the
Ajax.Request()
constructor (don't forget the
new
keyword!).
If you need to send parameters with your request, you'll have to do a bit more preparation before
calling
new Ajax.Request()
. Like jQuery, you can create an object to contain the parameter names
and values. For example, if you need to send a parameter called
username
with your request, you
can do something like this:
var parms = {
username: "jmcpeak"
};
options.parameters = parms;
When you send the request by creating a new
Ajax.Request
object, the parameters are added to the
URL before the request is sent to the server.
All callback functions are passed a parameter containing an
Ajax.Response
object, an object
that wraps around the native
XMLHttpRequest
object. It contains a variety of useful properties
for working with the server's response. It emulates the basic properties of
XMLHttpRequest
, such
as
readyState
,
responseText
,
responseXML
, and
status
. But it also exposes a few convenience
properties, as outlined in the following table.
propertY name
purpose
The
Ajax.Request
object used to make the request
request
A parsed JSON structure if the response's
Content‐Type
header is
application/json
responseJSON
The HTTP status text sent by the server
statusText
The native
XMLHttpRequest
object used to make the request
transport
Now that you've been given a crash course in Prototype's Ajax functionality, let's modify the Ajax
Form Validator.
revisiting the Form Validator with prototype
trY it out
Open your text editor and type the following:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chapter 17: Example 4</title>