First, test for the most recent version of the XHR
object; if that fails, try to use a fallback version. (See the section ???What Do All the Different
ActiveX Objects Mean???? for more information.)
With the constructor complete, you need to give the object a send() method so that you
can actually send requests to the server. I??™ve set it up to take two parameters: the URL that
you want to request and then an options object.
nNote Remember that passing in optional parameters using an object literal is a great way to keep the
code clear and concise.
Right now, there are only two optional properties: method (GET or POST) and callback.
The callback property gets called every time the readyState of the Ajax object changes. Once
the options are mapped to the internal _options object, open the URL, attach the event handler,
and then send the request.
nNote If you want to reuse this Ajax object to send another request, the onreadystatechange event has
to be declared after the open call; otherwise IE 5 or 6 will fail on every call after the first.
Now, let??™s take a look at how to use the fancy new Ajax object:
function processRequest(transport)
{
if(transport.readyState == 4)
{
var obj = transport.responseText.parseJSON();
}
}
You set up the callback function, which takes the transport as its one and only parameter.
Within the callback function, see whether the state is equal to 4, which indicates that the
object has properly returned a result.
Pages:
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185