AJAX Events (^) ❘ 213
load() method. First, because you want to load HTML, you need to remember what element you
want to load that HTML into.
var tree = $(this);
$(this) is assigned to a variable called tree so that you can reference the variable tree from within
the callback functions that you assign to the various options of the $.ajax() method. If you remem-
ber from Example 7-4, this refers to the element containing the arrows that appear beside
each folder. The $.ajax() method takes various options defi ned as an object literal, which are docu-
mented in Appendix G. You again defi ne the beforeSend, success, and error options that contain
functions that reveal and hide the activity indicator, but this time in the context of the AJAX request
that you’re making instead of globally.
If your request were successful, the rest of the code carries on like the code from Example 7-4. The
down.png'. The response variable in the success method contains the HTML text content of the
response containing the subfolders. If this were an XML request, you’d be working with an XML
object; if it were JSON, you’d be working with a JSON object. The HTML snippet is loaded into the
next sibling
ible with the show() method.
success : function(response, status, request)
{
$('div#folderActivity').hide();
tree.attr('src', arrow)
.next()
.html(response)
.show();
},
jQuery’s $.ajax() method allows for a great deal of request customization, which should be used
when the other AJAX methods just don’t provide the options that you need.
Sending a REST Request
The last example of using the $.ajax() method that I present is how to make and send a REST
request. Sending a REST request with jQuery is straightforward; you must confi gure the type,
contentType, dataType, and data properties to set up the REST call. Otherwise, your server must
also be properly confi gured to receive calls to a REST service. This will include setting the Access-
Control-Allow-Methods HTTP header on your server, which will allow HTTP request methods other
than GET and POST. Properly setting up and confi guring a web server to deliver a REST service is
outside the scope of this book. You can, however, examine what is required on the client side for
such a request utilizing jQuery’s $.ajax() method. This is demonstrated in the following document,
Example 7-9:<!DOCTYPE HTML>
<html lang='en'>
<head>
<meta http-equiv='X-UA-Compatible' content='IE=Edge' />
<meta charset='utf-8' />