(^180) ❘ CHAPTER 7 AJAX
The distinction between the POST and GET methods is mostly moot when it comes to making a
request originating from an AJAX script. Because the user is not involved with the request, the auto-
matic protection portion becomes unnecessary, which leaves only the semantic differences and the
limitations in length. For the most part, you can get away with making GET requests for everything,
which has been said to have a slight performance advantage over the POST method. Personally,
I tend to honor the semantic differences out of simple habit from years of working with forms in
client-side programming.
NOTE You can fi nd more information about the performance aspect on the
Yahoo Developer website at http://developer.yahoo.com/performance/rules
.html.
RESTful Requests
REST, or Representational State Transfer, makes it possible to distinguish between differing actions
based on the specifi ed HTTP transport method. So far you’ve learned about the GET and POST
HTTP transport methods. As stated in the last section, distinguishing between GET and POST
is mostly semantic because every HTTP server can handle GET and POST. REST is an architec-
tural decision in how you choose to implement your web services. Instead of GET and POST being
merely semantically different, you can choose to enforce technical constraints as well, for example,
a server-side application that behaves and handles data differently based on whether you use the
GET or POST method. In addition to GET and POST, a REST architecture can implement the PUT
and DELETE methods. Finally, in addition to those four methods, some implementations may take
things even further by defi ning additional methods, for example, PATCH.
The differences within which HTTP transport method you specify (GET, POST, PUT, or DELETE)
can be enforced on the server side. For example, you can send GET, POST, PUT, or DELETE
requests to the same URI location with information specifi ed in the body of the request as JSON,
and then the server-side application routes the request and executes different code based on which
method is specifi ed. Many developers simply use the URL to do the same thing and don’t go beyond
GET or POST. It has become increasingly more in fashion, however, to use HTTP to clearly defi ne
the purpose of a request, as well as to respond to a request using HTTP error codes. Using a REST
approach brings your AJAX applications more into the realm of defi ned standards by making use of
the features defi ned in the HTTP protocol that have been mostly ignored.
In the section “Sending a REST Request,” I present an example of how to send and receive data
with jQuery using a REST approach.
Formats Used to Transport Data with an AJAX Request
Although the name implies that you use XML to transport data with an AJAX request, this is
entirely optional. In addition to XML, there are two other common ways that data is transmitted
from the server to a client-side JavaScript application: JSON (or JavaScript Object Notation) and
HTML. You are not limited to these formats, however, because you can conceivably take any data