HTML and CSS Reference
Programmers. The initial project state can be downloaded off the topic's website 1
for your convenience.
12.2.2 Choosing the Interface Style
The first thing we need to decide is how we want to implement the request interface.
To make an informed decision, we need a quick reminder on how a basic XML-
HttpRequest works. The following shows the bare minimum of what needs to
be done (order matters).
1. Create an XMLHttpRequest object.
2. Call the open method with the desired HTTP verb, the URL, and a boolean
indicating whether the request is asynchronous or not; true means
3. Set the object's onreadystatechange handler.
4. Call the send method, passing in data if any.
Users of the high-level interface shouldn't have to worry about these details.
All we really need to send a request is a URL and the HTTP verb. In most cases the
ability to register a response handler would be useful as well. The response handler
should be available in two flavors: one to handle successful requests and one to
handle failed requests.
For asynchronous requests, the onreadystatechange handler is called
asynchronously whenever the status of the request is updated. In other words, this
is where the request eventually finishes, so the handler needs some way to access
the request options such as callbacks.
Before we can dive into the request API, we need a cross-browser way to obtain an
XMLHttpRequest object. The most obvious “Ajax” browser inconsistencies are
found in the creation of this very object.