Previous Page
Next Page

3.3. How to Decide on a Request Type

There are two main request types used in AJAX communication: POST and GET. These are the identical choices you have with forms in HTML, and the same rules apply. GET requests can be used when the URL performs no action. (For example, a URL such as index.php?section=main that displays the main section of a Web site is a good use for GET.) A URL such as index.php?action=delete&id=1 should never be used with GET. The main reasons for this go back to the HTTP standard, which suggests that GET requests should not perform permanent actions and are not allowed to be cached by proxies and other infrastructure. Some Web accelerator products also prefetch GET URLs from pages, which causes huge problems when they hit a URL that deletes a record. GET requests also have the drawback of potentially limiting the amount of data that the client can send to the server; current HTTP specs don't limit size, but many servers and browsers limit the size to around 2,000 characters. POST requests aren't allowed to be cached by proxies or prefetched, and thus are much safer than GET requests.

In most AJAX setups, especially any using an RPC approach, only POST requests should be used. While some of the requests might be just to load new data, it's generally too hard to keep track of which type of request is which. In a document-centric approach, GET requests might be useful, especially when you're using AJAX to load in chunks of static HTML or XML content. If you go with an approach like that, you'll want to use a good naming convention to help keep track of which type of request is needed for each URL.


Previous Page
Next Page