(^220) ❘ CHAPTER 7 AJAX
Summary
This chapter took you on a tour of jQuery’s built-in AJAX capabilities. You saw the differences
between a GET and a POST HTTP request, learning that a GET request has a limit on its length, in
addition to being semantically suited for requests that result in no lasting modifi cation or effect on
the server. POST requests, in contrast, should be reserved for requests that shouldn’t be arbitrarily
repeated and do have some kind of lasting impact on the server. In terms of AJAX, GET requests have
a slight performance advantage. In addition, you can take advantage of REST to make your requests
even more semantic, adding method verbs such as ADD or DELETE to the existing GET and POST.jQuery offers the $.get() method to make GET requests and the $.post() method to make POST
requests. When you work with XML, jQuery makes it super-easy to extract information from an
XML document by giving you full access to jQuery’s various selection methods for querying your
XML responses. The JSON format is easier to work with, but extra care must be taken to ensure
that you don’t make yourself vulnerable to a cross-site forgery. For working with JSON data, jQuery
offers the $.getJSON() method.You can use the load() method to insert HTML snippets requested from the server into elements
that you select with jQuery.I described how to load script asynchronously with jQuery’s $.getScript() method. This was
demonstrated by loading the jQuery UI API on demand, which was applied to the calendar you
made in Example 7-5, and provided a nifty animated effect when you clicked the various days in
the calendar.I talked about jQuery’s AJAX events and described the different ways you can use AJAX events
to add an activity indicator to the folder tree example, creating Example 7-6, Example 7-7, and
Example 7-8 demonstrating the different ways that AJAX events can be utilized.Finally, I demonstrated how to use jQuery’s $.ajax() method to create a request to a server imple-
menting REST services.EXERCISES
- For an AJAX request, is there any difference between a GET and a POST HTTP request?
- What does a REST service provide?
- How would you provide extra data with a request using jQuery’s $.get() method?
- How would you access a JSON object in a callback function provided to the $.getJSON()
method? - Given the following XML, how would you access the contents of the
element,
assuming you used jQuery’s $.get() method to request the XML document?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>Yes!</response>