Programming Microsoft ASP.NET 4 - Dino Esposito [482]
The async parameter indicates whether the call has to go asynchronously or not. The cache Boolean parameter indicates whether you want the library to cache the response for future access to the same URL. Ajax calls are always cached by default except for when the data type is JSONP or script.
The $.ajax function supports several callbacks. The beforeSend callback is invoked just before sending the request out. The callback receives the settings of the call and represents your last chance to modify the call. The complete callback is invoked as soon as the response is received and regardless of the outcome. The callback receives a description of the HTTP status of the request and indicates whether the request completed successfully, resulted in an error, timed out, or pointed to a resource that was not modified. The callback won’t receive the actual response, if there is any. Past the complete callback, the library fires either the success or error callback, depending on the context. The success callback receives the response sent over the wire by the server. The error callback gets a code for the type of error (timeout, parse, or error) and an exception object that provides, if possible, more details about the failure.
On top of the ajax function, a number of shortcut functions have been created that make it simpler for developers to place certain specific types of calls, such as calls for getting a script file or a JSON string. The get and post functions also exist to perform plain HTTP GET and POST requests.
Global Ajax Event Handlers
The jQuery library provides a bunch of global handlers for Ajax events so that you can register your handlers that are invoked for each Ajax operation regardless of the code that triggers it. You can add handlers for the events in Table 21-8.
Table 21-8. Global Ajax Events
Event
Description
ajaxComplete
Fires upon completion of any Ajax request, regardless of the outcome
ajaxError
Fires when an Ajax call fails
ajaxSend
Fires when an Ajax request is sent
ajaxStart
Fires when an Ajax request begins being processed
ajaxStop
Fires when no pending Ajax requests are left
ajaxSuccess
Fires when an Ajax request completes with success
You can have multiple handlers for each of these events. If multiple handlers are registered, all of them are invoked in order.
Getting Scripts
The getScript function requires you to provide the URL for the script file and an optional callback to execute upon downloading the script. Here’s the signature of the function:
$.getScript(url, succeededCallback)
The interesting thing about the function is that the downloaded script is processed by jQuery right after download. This means that in the callback, you can already start using objects and features available in the script:
The request being placed for the script is an HTTP GET. Keep in mind that if you need to tweak the request beyond the hardcoded settings of the getScript function, you better resort to the ajax function.
Getting JSON
The getJSON function is ideal for invoking an HTTP endpoint that is expected to return a JSON-encoded string. Here’s the signature of the function:
$.getJSON(url, inputData, succeededCallback)
When you make a JSON request, you might need to send some data over to the remote server to guide the generation of the response. The second argument to getJSON represents the input you intend to pass. Here’s an example:
var playerId = 1;
$.getJSON("/yourServer/Player/Details", playerId, function(jsonData) {
// Start using the information stored in the downloaded object here