JQuery_ Novice to Ninja - Earle Castledine [150]
data
To pass data to the server, you specify a string, JavaScript array, or object for the data setting. If you pass in an array or object (and you haven’t disabled the processData flag), the data will be serialized with the jQuery.params utility function. This takes the input and coverts it into a query-string format (very handy)! If it’s a GET request, the string will be appended to the URL.
dataType
Where contentType set the content type of the data you were sending to the server, the dataType parameter sets the type of data you’re expecting to receive from the server. The default type is “everything” (*/*), but you could specify xml, html, script, json, or text—in which case jQuery will use the appropriate content type string.
jsonp
When you make JSONP calls (which allow cross-domain requests) it’s expected that the callback function name will be callback. If the service you’re using requires a different name, you can define it with the jsonp setting.
password
Authentication requests require your username and password. You can specify the latter in the password setting.
scriptCharset
The scriptCharset property allows you to specify the character set of script tags injected by script or jsonp Ajax calls.
timeout
The timeout parameter has the honor of being the only ajax setting that accepts a number: it defines how many milliseconds need to elapse before aborting an Ajax request.
type
One of the most important settings for an Ajax request, the type property defines the HTTP request type: GET, POST, PUT, or DELETE.
url
The other most important setting along with type, the url string defines the address of the location you want to call.
username
Last but not least, the username option specifies the username to send with any authentication requests.
Callbacks and Functions
Finally, there are a bunch of callbacks and functions you can define to tweak your request and handle events that occur during the request’s life cycle.
The event handlers have already been covered in Chapter 6. The complete handler will fire whenever an $.ajax call completes—regardless of success or failure—so it’s a good place to clean up any loose ends. The error handler is called whenever the call fails, and the success handler fires whenever it completes correctly.
As well as the event callbacks, there are also a handful of functions existing as hooks; these let you modify various parts of a request on a call-by-call basis. The beforeSend function fires before the send message is executed, and gives you a place to modify the request if you require. Your handler is given the request object and the current jQuery object. This is a common place to modify the request headers when it’s required. Also, it’s your last chance to stop anything from happening: if you return false from this function, the request will be aborted.
At the lowest level, the magic of Ajax comes from the browser’s implementation of the XMLHTTPRequest (or XHR for short) object. This is the fellow that lets us communicate with the server from the client. By default, jQuery looks for the appropriate XHR object and uses this for any Ajax calls. You can modify this if you want to augment or replace it by specifying your own xhr function. The function needs to return the object that should be used.
The last hook that’s available is the dataFilter function. This is called after a request successfully returns, and is a place for you to make sure the response data is okay. If you need to do any data sanitizing, this is the spot. The dataFilter function is passed the raw response data and the type; you should return the data once you’ve processed it so the request cycle can continue.
$.support Options
In the old days, we’d use browser sniffing to determine which version of which browser was being used, and adjust our code to work around known bugs. Today, this