With the XMLHttpRequest object you can update parts of a web page, without reloading the whole page.
The XMLHttpRequest object is used to exchange data with a server behind the scenes.
The XMLHttpRequest object is the developers dream, because you can:
All modern browsers (IE7+, Firefox, Chrome, Safari, and Opera) have a built-in XMLHttpRequest object.
Syntax for creating an XMLHttpRequest object:
Old versions of Internet Explorer (IE5 and IE6) uses an ActiveX Object:
To handle all modern browsers, including IE5 and IE6, check if the browser supports the XMLHttpRequest object. If it does, create an XMLHttpRequest object, if not, create an ActiveXObject:
To send a request to a server, we use the open() and send() methods of the XMLHttpRequest object:
|open(method,url,async)||Specifies the type of request, the URL, and if the request should be handled asynchronously or not.
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)||Sends the request off to the server.
string: Only used for POST requests
GET is simpler and faster than POST, and can be used in most cases.
However, always use POST requests when:
The url parameter of the open() method, is an address to a file on a server:
The file can be any kind of file, like .txt and .xml, or server scripting files like .asp and .php (which can perform actions on the server before sending the response back).
To send the request asynchronously, the async parameter of the open() method has to be set to true:
Sending asynchronously requests is a huge improvement for web developers. Many of the tasks performed on the server are very time consuming.
When using async=true, specify a function to execute when the response is ready in the onreadystatechange event:
To use async=false, change the third parameter in the open() method to false:
Using async=false is not recommended, but for a few small requests this can be ok.
Note: When you use async=false, do NOT write an onreadystatechange function - just put the code after the send() statement:
To get the response from a server, use the responseText or responseXML property of the XMLHttpRequest object.
|responseText||get the response data as a string|
|responseXML||get the response data as XML data|
If the response from the server is not XML, use the responseText property.
The responseText property returns the response as a string, and you can use it accordingly:
If the response from the server is XML, and you want to parse it as an XML object, use the responseXML property:
Request the file cd_catalog.xml and parse the response:
When a request to a server is sent, we want to perform some actions based on the response.
The onreadystatechange event is triggered every time the readyState changes.
The readyState property holds the status of the XMLHttpRequest.
Three important properties of the XMLHttpRequest object:
|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
404: Page not found
In the onreadystatechange event, we specify what will happen when the server response is ready to be processed.
When readyState is 4 and status is 200, the response is ready:
Note: The onreadystatechange event is triggered four times, one time for each change in readyState.
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.