HTML and CSS Reference
In-Depth Information
Implementation, shown in Listing 12.32, is once again a one-liner.
Listing 12.32 Calling send
function get(url) {
/* ... */
transport.send();
}
All lights are green once again. Notice how stubXMLHttpRequest is already
paying off. We didn't need to update any of the other stubbed tests even when we
called a new method on the XMLHttpRequest object, seeing as they all get it from
the same source.
12.4.4 Handling the State Changes
ajax.get is now complete in an extremely minimalistic way. It sure ain't done, but it
could be used to send a GET request to the server. We will turn our focus to the
onreadystatechange handler in order to allow users of the API to subscribe
to the success and failure events.
The state change handler is called as the request progresses. Typically, it will be
called once for each of these 4 states (from the W3C XMLHttpRequest spec draft.
Note that these states have other names in some implementations):
1. OPENED , open has been called, setRequestHeader and send may be
called.
2. HEADERS RECEIVED , send has been called, and headers and status are
available.
3. LOADING , Downloading; responseText holds partial data.
4. DONE , The operation is complete.
For larger responses, the handler is called with the loading state several times
as chunks arrive.
12.4.4.1 Testing for Success
To reach our initial goal, we really only care about when the request is done. When
it is done we check the request's HTTP status code to determine if it was successful.
We can start by testing the usual case of success: ready state 4 and status 200.
Listing 12.33 shows the test.
 
Search WWH ::




Custom Search