English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The XMLHttpRequest object is used to exchange data with the server.
All modern browsers (Chrome, Firefox, IE7 +Edge, Safari, Opera) all have built-in XMLHttpRequest objects.
XMLHttpRequest allows asynchronous updates of the web page by exchanging data with the background web server.
You can retrieve data from a URL without refreshing the entire page.
This allows the web page to update a part of the page without interrupting the user's operation.
Before executing AJAX communication between the client and the server, the first thing to do is to instantiate an XMLHttpRequest object, as shown below:
Syntax:
var request = new XMLHttpRequest();
var httpRequest = new XMLHttpRequest();Test See‹/›
For security reasons, modern browsers do not allow cross-domain access.
This means that both the web page and the requested files it tries to load must be on the same server.
w3Examples on codebox.com are all opened in the w3All open request files in the codebox.com domain.
If you want to use the above example on one of your own web pages, the requested file must be located on your own server.
Old versions of Internet Explorer (6and earlier versions) use ActiveX objects instead of XMLHttpRequest objects.
Syntax:
var request = new ActiveXObject("Microsoft.XMLHTTP");
To handle IE6and earlier versions, please check if the browser supports the XMLHttpRequest object, otherwise create an ActiveX object:
var httpRequest; if (window.XMLHttpRequest) { // The mainstream browsers (Chrome, Mozilla, Safari, IE7+, ...) httpRequest = new XMLHttpRequest(); else if (window.ActiveXObject) { // <= IE 6 or earlier IE versions httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); }Test See‹/›
The following table lists all properties of the XMLHttpRequest object:
Property | Description |
---|---|
onreadystatechange | Define the Function to Be Called When the readyState Property Changes |
readyState | Maintain the XMLHttpRequest Status: 0:Request Not Initialized 1:Server Connection Established 2:Received Request 3:Processing Request 4:Request Completed and Response Ready |
responseText | Return Response Data as a String |
responseXML | Return Response Data as XML Data |
status | Return the Request Status Number: 200:"OK" 403:"Forbidden" 404:"Not Found" For the complete list, please visitHTTP Status Code Reference |
statusText | Return Status Text (for example, "OK" or "Not Found") |
The following table lists all methods of the XMLHttpRequest object:
Method | Description |
---|---|
new XMLHttpRequest() | Create a New XMLHttpRequest Object |
abort() | Cancel the Current Request |
getAllResponseHeaders() | Return Title Information |
getResponseHeader() | Return Specific Header Information |
open(method, url, async) | Specify Request method:Request TypeGETorPOST url:File Location async:true (asynchronous) or false (synchronous) |
send() | Send the request to Used forGETThe server of the request |
send(string) | Send the request to the server. Used forPOSTRequest |
setRequestHeader(header, value) | Set HTTP Header for Request header:Specify Header Name value:Specify Header Value |