AJAX - The Complete Reference

(avery) #1

214 Part II: Applied Ajax^


Prototype also uses the options object to set the various callbacks that are invoked in
the course of an Ajax request. Table 5-8 presents a quick overview of each one; it should
look very similar to the callbacks that this book’s library uses.

NNOTEOT E Be aware that, when using onStatusCode handlers, your onSuccess and onFailure
won't fire because onStatusCode handler like on200 takes precedence.

Options Description
asynchronous Defines if the communication should be asynchronous or not. A value of true
indicates asynchronous while a value of false indicates synchronous. Like a
raw XHR, it is true by default.
contentType Indicates the MIME type of any provided data to be used in the Content-Type
header of a request. By default, it is set to application/x-www-form-
urlencoded, though it could be overridden to other data types. As seen in
Chapter 4, such overrides, while possible, do have consequences in terms of
encoding/decoding effort.
encoding Sets the character set encoding for the request. UTF-8 is the fortunate default
here. As discussed in Chapter 4, modifications to the request format may do
nothing in some implementations. In other situations without ser ver translation,
you may run into significant problems when setting this value.
method Defines the HTTP method used, generally GET or POST. Interestingly, Prototype
as of version 1.5 defaults to POST, unlike most other libraries. If you use other
HTTP methods such as put or delete, it will modify the request to a post and
add a _method parameter to the transmission. This action is for the benefit for
the Ruby on Rails environment, which is willing to interpret HTTP methods from
such a parameter.
parameters Specifies the payload for the request transmitted either in the quer y string in
the case of GET request, or the message body in the case of a POST request.
The parameter can be specified as a string that should be in the property URL-
encoded name-value pair format. It is also possible to pass in an object that
has properties and values that will be serialized into a payload automatically.
postBody An alternate approach to specify the body of a POST request. If this option is
not set, the parameters value is used.
requestHeaders Specifies the headers that should be sent with the request. You can pass in an
object with headers in the form of properties and values like so:
{ User-agent : "AjaxBrowser", X-Name : "Thomas" }. Or you can
do it in an array with even values being names and odd values being values
like so: ["User-Agent" , "AjaxBrowser" , "X-Name" , "Thomas"].
By default Prototype will send X-Requested-With: XMLHttpRequest and
X-Prototype-Version: 1.5.1.1 (or whatever the value of the librar y is in
use). These values could of course be over written. Other appropriate headers
may be set based upon the value of contentType and encoding options if
they are set.

TABLE 5-7 Prototype 1.5 Ajax Request Options
Free download pdf