XML DOM - HttpRequest object

# The XMLHttpRequest Object

« Previous
Next Chapter »

With the XMLHttpRequest object you can update parts of a web page, without reloading the whole page.

## Try it Yourself - Examples

A simple XMLHttpRequest example
Create a simple XMLHttpRequest, and retrieve data from a TXT file.

Retrieve header information with getAllResponseHeaders()
Retrieve header information of a resource (file).

Retrieve specific header information with getResponseHeader()
Retrieve specific header information of a resource (file).

Retrieve the content of an ASP file
How a web page can communicate with a web server while a user type characters in an input field.

Retrieve content from a database
How a web page can fetch information from a database with the XMLHttpRequest object.

Retrieve the content of an XML file
Create an XMLHttpRequest to retrieve data from an XML file and display the data in an HTML table.

## The XMLHttpRequest Object

The XMLHttpRequest object is used to exchange data with a server behind the scenes.

The XMLHttpRequest object is the developers dream, because you can:

- Update a web page without reloading the page
- Request data from a server after the page has loaded
- Receive data from a server after the page has loaded
- Send data to a server in the background

## XMLHttpRequest Object Methods

Method | Description
--- | ---
abort() | Cancels the current request
getAllResponseHeaders() | Returns header information
getResponseHeader() | Returns specific header information
open(method,url,async,uname,pswd) | Specifies the type of request, the URL, if the request should be handled asynchronously or not, and other optional attributes of a request method: the type of request: GET or POST url: the location of the file on the server async: true (asynchronous) or false (synchronous)
send(string) | send(string) Sends the request off to the server. string: Only used for POST requests
setRequestHeader() | Adds a label/value pair to the header to be sent

## XMLHttpRequest Object Properties

Property | Description
--- | ---
onreadystatechange | Stores a function (or the name of a function) to be called automatically each time the readyState property changes

readyState

Holds the status of the XMLHttpRequest. Changes from 0 to 4:
0: request not initialized
1: server connection established
2: request received
3: processing request
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")

« Previous
Next Chapter »

### WEB HOSTING

UK Reseller Hosting

### WEB BUILDING

FREE Website BUILDER Free HTML5 Templates

### W3SCHOOLS EXAMS

HTML, CSS, JavaScript, PHP, jQuery, XML, and ASP Certifications

### SHARE THIS PAGE

### Color Picker

### HTML/CSS

HTML Tutorial HTML5 Tutorial CSS Tutorial CSS3 Tutorial Bootstrap Tutorial

### JavaScript

JavaScript Tutorial jQuery Tutorial jQuery Mobile Tutorial AngularJS Tutorial AJAX Tutorial JSON Tutorial

### Graphics

Canvas Tutorial SVG Tutorial Google Maps Tutorial

### Server Side

SQL Tutorial PHP Tutorial ASP Tutorial ASP.NET Tutorial VBScript Tutorial AppML Tutorial

### Web

Web Building Web Statistics Web Certification

### XML

XML Tutorial DTD Tutorial Schema Tutorial XML DOM Tutorial XPath Tutorial XSLT Tutorial XQuery Tutorial XSL-FO Tutorial RSS Tutorial WSDL Tutorial

### HTML/CSS

HTML Tag Reference HTML Event Reference HTML Color Reference CSS 1,2,3 Reference Bootstrap Reference

### JavaScript

JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference Google Maps Reference

### Server Side

PHP Reference SQL Reference ASP Reference VBScript Reference Razor Reference ASP.NET Reference

### XML

XML DOM Reference XSLT Reference XSL-FO Reference XPath Reference XQuery Reference Schema Reference RSS Reference SVG Reference

### HTML Charsets

HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8

### HTML/CSS

HTML Examples CSS Examples

### Browser Scripting

JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples

### Server Scripting

PHP Examples ASP Examples VBScript Examples Razor Examples .NET Examples

### XML Languages

XML Examples DTD Examples XML DOM Examples XSL Examples XSLT Examples XPath Examples XQuery Examples Schema Examples RSS Examples SVG Examples