The XMLHttpRequest Object
The XMLHttpRequest object is supported in Internet Explorer
5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 9, and Netscape 7.
What is an HTTP Request?
With an HTTP request, a web page can make a request to, and get a response from a web
server - without reloading the page. The user will stay on the same page, and he
or she will
not notice that scripts might request pages, or send data to a
server in the background.
By using the XMLHttpRequest object, a web developer can change a page with data from the server
after the page has loaded.
Google Suggest
is using the XMLHttpRequest object to create a very dynamic web interface:
When you start typing in Google's search box, a JavaScript sends the letters off
to a server and the server returns a list of suggestions.
Is the XMLHttpRequest Object a W3C Standard?
The XMLHttpRequest object is a JavaScript object, and is not specified in any W3C
recommendation.
However, the W3C DOM
Level 3 "Load and Save" specification contains some similar functionality, but these
are not implemented in any browsers yet. So, at the moment, if you need to send
an HTTP request from a browser, you will have to use the XMLHttpRequest object.
Creating an XMLHttpRequest Object
For Mozilla, Firefox, Safari, Opera, and Netscape:
var xmlhttp=new XMLHttpRequest()
|
For Internet Explorer:
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
|
Example
<script type="text/javascript">
var xmlhttp
function loadXMLDoc(url)
{
xmlhttp=null
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}
else
{
alert("Your browser does not support XMLHTTP.")
}
}
function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
{
// if "OK"
if (xmlhttp.status==200)
{
// ...some code here...
}
else
{
alert("Problem retrieving XML data")
}
}
}
</script>
|
Try it yourself using JavaScript
The syntax is a little bit different in VBScript:
Try it yourself using
VBScript
Note: An important property in the example above is the
onreadystatechange property. This property is an event handler which is
triggered each time the state of the request changes. The states run from 0 (uninitialized)
to 4 (complete). By having the function xmlhttpChange() check for the
state changing, we can tell when the process is complete and continue only if
it has been successful.
Why are we Using Async in our Examples?
All the examples here use the async mode (the third parameter of open() set
to true).
The async parameter specifies
whether the request should be handled asynchronously or not. True means
that script
continues to run after the send() method, without waiting for a response
from the server. false
means that the script waits for a response before continuing script
processing. By setting this parameter to false, you run the risk of having your
script hang if there is a network or server problem, or if the request is
long (the UI locks while the request is being made) a user may even
see the "Not Responding" message. It is safer to send asynchronously and design your code around
the onreadystatechange event!
More Examples
Load a
textfile into a div element with XML HTTP (JavaScript)
Make a
HEAD request with XML HTTP (JavaScript)
Make a
specified HEAD request with XML HTTP (JavaScript)
List data from an XML file with XML HTTP (JavaScript)
The XMLHttpRequest Object Reference
Methods
| Method |
Description |
| abort() |
Cancels the current request |
| getAllResponseHeaders() |
Returns the complete set of http headers as a string |
| getResponseHeader("headername") |
Returns the value of the specified http header |
| open("method","URL",async,"uname","pswd") |
Specifies the method, URL, and other optional attributes of a
request The method parameter can have a value of "GET", "POST",
or "PUT" (use "GET" when requesting data and use "POST"
when sending data (especially if the length of the data is
greater than 512 bytes.
The URL parameter may be either a relative or
complete URL.
The async parameter specifies
whether the request should be handled asynchronously or not. true means
that script
processing carries on after the send() method, without waiting for a response. false
means that the script waits for a response before continuing script
processing |
| send(content) |
Sends the request |
| setRequestHeader("label","value") |
Adds a label/value pair to the http header to be sent |
Properties
| Property |
Description |
| onreadystatechange |
An event handler for an event that fires at every state change |
| readyState |
Returns the state of the object: 0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete |
| responseText |
Returns the response as a string |
| responseXML |
Returns the response as XML. This property returns an XML document
object, which can be examined and parsed using W3C DOM node tree methods and
properties |
| status |
Returns the status as a number (e.g. 404 for "Not Found" or 200 for
"OK") |
| statusText |
Returns the status as a string (e.g. "Not Found" or
"OK") |
|