jQuery in Action
250
CHAPTER 8
Talk to the server with Ajax
Looks simple, doesn't it? But don't be deceived. The
options
parameter can spec-
ify a large range of values that can be used to tune the operation of this function.
These options (in order of the likelihood of their use) are defined in table 8.2.
These options (in order of the likelihood of their use) are defined in table 8.2.
Command syntax: $.ajax
$.ajax(options)
Initiates an Ajax request using the passed options to control how the request is made and
callbacks notified.
callbacks notified.
Parameters
options
(Object) An object instance whose properties define the parameters to this
operation. See table 8.2 for details.
operation. See table 8.2 for details.
Returns
The XHR instance.
The XHR instance.
Table 8.2 Options for the
$.ajax()
utility function
Name
Type
Description
url
String
The URL for the request.
type
String
The HTTP method to use. Usually either POST or GET. If omitted, the default
is GET.
is GET.
data
Object
An object whose properties serve as the query parameters to be passed to
the request. If the request is a GET, this data is passed as the query string.
If a POST, the data is passed as the request body. In either case, the
encoding of the values is handled by the $.ajax() utility function.
the request. If the request is a GET, this data is passed as the query string.
If a POST, the data is passed as the request body. In either case, the
encoding of the values is handled by the $.ajax() utility function.
dataType
String
A keyword that identifies the type of data that's expected to be returned by the
response. This value determines what, if any, post-processing occurs upon the
data before being passed to callback functions. The valid values are as follows:
response. This value determines what, if any, post-processing occurs upon the
data before being passed to callback functions. The valid values are as follows:
xml
--The response text is parsed as an XML document and the result-
ing XML DOM is passed to the callbacks.
html
--The response text is passed unprocessed to the callbacks func-
tions. Any <script> blocks within the returned HTML fragment are
evaluated.
evaluated.
json
--The response text is evaluated as a JSON string, and the result-
ing object is passed to the callbacks.
jsonp
--Similar to jason except that remote scripting is allowed,
assuming the remote server supports it.
script
--The response text is passed to the callbacks. Prior to any
callbacks being invoked, the response is processed as a JavaScript
statement or statements.
statement or statements.
text
--The response text is assumed to be plain text.
continued on next page