Please navigate to the bottom of the page for Table of Contents

Saturday, June 4, 2011

jQuery AJAX functions part 3–ajax()

The $.ajax() function underlies all Ajax requests sent by jQuery. This is by far the most complex function in the jQuery library. In fact, the jQuery official documentation marks this as a low-level function and recommends using one of the high level functions (such as get(), post(), etc.). This function accepts a single argument: a set of key/value pairs that configure the Ajax request.

From an interview perspective, it is important to understand how the other higher level functions map to this function.

jQuery.Get() call is equivalent to the following .ajax() call:

$.ajax({
url: url,
data: data,
success: success,
dataType: dataType
});



jQuery.Post() call maps out to the following .ajax() call:


$.ajax({
type: 'POST',
url: url,
data: data,
success: success
dataType: dataType
});



jQuery.GetJSON() maps to


$.ajax({
url: url,
dataType: 'json',
data: data,
success: callback
});



As you can see from the above, a consistent pattern is visible. If a type is not specified, GET is used. A url is almost always required. To pass in data, you use the data name. And, you can have a callback for success. Of course, there are a lot more parameters that can be configured. A few key ones are crossDomain for cross domain access for JSONP, error for error handling function, password for authenticated requests, and so on.

No comments:

Post a Comment