jQuery in Action
Taking full control of an Ajax request
251
The server resource is responsible for setting the appropriate content-type
response header.
If this property is omitted, the response text is passed to the callbacks with-
out any processing or evaluation.
response header.
If this property is omitted, the response text is passed to the callbacks with-
out any processing or evaluation.
timeout
Number
Sets a timeout for the Ajax request in milliseconds. If the request does not
complete before the timeout expires, the request is aborted and the error
callback (if defined) is called.
complete before the timeout expires, the request is aborted and the error
callback (if defined) is called.
global
Boolean
Enables (if true) or disables (if false) the triggering of so-called global
functions. These are functions that can be attached to elements that trigger
at various points or conditions during an Ajax call. We'll be discussing them
in detail in section 8.8. If omitted, the default is to enable the triggering of
global functions.
functions. These are functions that can be attached to elements that trigger
at various points or conditions during an Ajax call. We'll be discussing them
in detail in section 8.8. If omitted, the default is to enable the triggering of
global functions.
contentType
String
The content type to be specified on the request. If omitted, the default is
application/x-www-form-urlencoded, the same type used as the default for
form submissions.
application/x-www-form-urlencoded, the same type used as the default for
form submissions.
success
Function
A function invoked if the response to the request indicates a success status
code. The response body is returned as the first parameter to this function
and formatted according to the specification of the dataType property.
The second parameter is a string containing a status value--in this case,
always success.
code. The response body is returned as the first parameter to this function
and formatted according to the specification of the dataType property.
The second parameter is a string containing a status value--in this case,
always success.
error
Function
A function invoked if the response to the request returns an error status
code. Three arguments are passed to this function: the XHR instance, a
status message string (in this case, always error), and an optional excep-
tion object returned from the XHR instance.
code. Three arguments are passed to this function: the XHR instance, a
status message string (in this case, always error), and an optional excep-
tion object returned from the XHR instance.
complete
Function
A function called upon completion of the request. Two arguments are
passed: the XHR instance and a status message string of either success or
error. If either a success or error callback is also specified, this function is
invoked after the callback is called.
passed: the XHR instance and a status message string of either success or
error. If either a success or error callback is also specified, this function is
invoked after the callback is called.
beforeSend
Function
A function invoked prior to initiating the request. This function is passed the
XHR instance and can be used to set custom headers or to perform other
pre-request operations.
XHR instance and can be used to set custom headers or to perform other
pre-request operations.
async
Boolean
If specified as false, the request is submitted as a synchronous request,
By default, the request is asynchronous.
By default, the request is asynchronous.
processData
Boolean
If set to false, prevents the data passed from being processed into URL-
encoded format. By default, the data is URL-encoded into a format suitable
for use with requests of type application/x-www-form-urlencoded.
encoded format. By default, the data is URL-encoded into a format suitable
for use with requests of type application/x-www-form-urlencoded.
ifModified
Boolean
If true, allows a request to succeed only if the response content has not
changed since the last request according to the Last-Modified header. If
omitted, no header check is performed.
changed since the last request according to the Last-Modified header. If
omitted, no header check is performed.
Table 8.2 Options for the
$.ajax()
utility function (continued)
Name
Type
Description