Java Reference
In-Depth Information
{
var oHttp = new XMLHttpRequest();
return oHttp;
}
else if (window.ActiveXObject)
{
var versions =
[
“MSXML2.XmlHttp.6.0”,
“MSXML2.XmlHttp.3.0”
];
for (var i = 0; i < versions.length; i++)
{
try
{
var oHttp = new ActiveXObject(versions[i]);
return oHttp;
}
catch (error)
{
//do nothing here
}
}
}
return null;
}
This new code fi rst checks to see if
window.XMLHttpRequest
exists. If it does, then the function creates
an
XMLHttpRequest
object with the
XMLHttpRequest
constructor. If not, the code checks for
window
.ActiveXObject
for IE 5 and 6 and tries to create an object with the latest
XMLHttp
version. If no
XMLHttpRequest
object can be created any browser, then the function returns
null
.
The order in which browsers are tested is important; test for
window.XMLHttpRequest
fi rst because
IE 7+ supports both
window.XMLHttpRequest
and
window.ActiveXObject
.
Regardless of the user's browser, if it supports
XMLHttpRequest
, this revised function creates an
XMLHttpRequest
object.
Using the XMLHttpRequest Object
Once you create the
XMLHttpRequest
object, you are ready to start requesting data with it. The fi rst step
in this process is to call the
open()
method to initialize the object.
oHttp.open(requestType, url, async);
This method accepts three arguments. The fi rst,
requestType
, is a string value consisting of the type
of request to make. The values can be either
GET
or
POST
. The second argument is the URL to send the
request to, and the third is a
true
or
false
value indicating whether the request should be made in
asynchronous or synchronous mode. For more on synchronous and asynchronous modes, see