XMLHttpRequest Object
Using the XMLHttpRequest object, you can update a part of a web page without reloading the entire page.
Try It Yourself - Examples
A Simple XMLHttpRequest Example
Retrieve Header Information with getAllResponseHeaders()
Retrieve Specified Header Information with getResponseHeader()
Retrieve Content of an ASP File
Retrieve Content from a Database
Retrieve Content of an XML File
XMLHttpRequest Object
The XMLHttpRequest object is used to exchange data with a server behind the scenes.
The XMLHttpRequest object is a developer's dream because you can:
- Update a web page without reloading the page
- Request data from the server after the page has loaded
- Receive data from the server after the page has loaded
- Send data to the server in the background
XMLHttpRequest Object Methods
Method | Description |
---|---|
abort() | Cancels the current request. |
getAllResponseHeaders() | Returns header information. |
getResponseHeader() | Returns the specified header information. |
open(method,url,async,uname,pswd) | Specifies the type of request, URL, whether the request should be handled asynchronously, and other optional attributes. <br> <br>method: Type of request: GET or POST <br>url: Location of the file on the server <br>async: true (asynchronous) or false (synchronous) |
send(string) | Sends the request to the server. <br> <br>string: Only used for POST requests |
setRequestHeader() | Adds a label/value pair to the headers to be sent. |
XMLHttpRequest Object Properties
Property | Description |
---|---|
onreadystatechange | Stores a function (or the name of the function) to be called automatically each time the readyState property changes. |
readyState | Holds the status of the XMLHttpRequest. Changes from 0 to 4: <br>0: Request not initialized <br>1: Server connection established <br>2: Request received <br>3: Processing request <br>4: Request finished and response is ready |
responseText | Returns the response data as a string. |
responseXML | Returns the response data as XML data. |
status | Returns the status number (e.g., "404" for "Not Found" or "200" for "OK"). |
statusText | Returns the status text (e.g., "Not Found" or "OK"). |