Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML DOM - HttpRequest Object

The XMLHttpRequest Object


With the XMLHttpRequest object, you can update part of a web page without reloading the entire page.


Try it - Example

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

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

Retrieve a specified header with getResponseHeader()
Retrieve a specified header of a resource (file).

Retrieve the content of an ASP file
How a web page communicates with a web server when the user types characters into an input field.

Retrieve content from a database
How a web page extracts information from a database through 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 adeveloper's dream, because you can:

XMLHttpRequest Object Methods

Method Description
abort() Aborts the current request.
getAllResponseHeaders() Returns the header information.
getResponseHeader() Returns the specified header information.
open(method,url,async,uname,pswd) Specifies the type of request, the URL, whether the request should be processed asynchronously, and other optional properties of the request.
 method:请求的类型:GET 或 POST       

 url:文件在服务器上的位置       

 async:true(异步)或 false(同步) |

| send(string) | Sends the request to the server.

 string:仅用于 POST 请求 |

| setRequestHeader() | Adds a label/value pair to the headers to be sent. |

XMLHttpRequest Object Properties

property Description
onreadystatechange Stores the 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:请求未初始化       

 1:服务器建立连接       

 2:收到的请求       

 3:处理请求       

 4:请求完成和响应准备就绪 |

| responseText | Returns the response data as a string. | | responseXML | Returns the response data as XML data. | | status | Returns the status number (for example, "404" for "Not Found" or "200" for "OK"). | | statusText | Returns the status text (such as "Not Found" or "OK"). |