Latest web development tutorials

XML DOM - HttpRequest objects

Through XMLHttpRequest object, you can update a section of a web page without reloading the entire page.


Examples

Try - Example

A simple instance of XMLHttpRequest
Create a simple XMLHttpRequest, retrieve data from TXT file.

By getAllResponseHeaders () to retrieve header information
To retrieve the resource (document) header information.

() Retrieves the specified header information getResponseHeader
To retrieve the resource (file) to specify the header information.

Content retrieval ASP file
When users type characters in the input field, how Web pages communicate with the Web server.

Retrieve content from the database
Web page how to extract information from the database through the XMLHttpRequest object.

Content retrieval XML file
Create an XMLHttpRequest to retrieve data from the XML file and display the data in an HTML table.


XMLHttpRequest Object

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

XMLHttpRequest object is adeveloper's dream, because you can:

  • Update web page without reloading the page
  • After the page is loaded from the server request data
  • After the page has loaded data received from the server
  • Sending data to the server in the background

XMLHttpRequest Object Methods

方法 描述
abort() 取消当前的请求。
getAllResponseHeaders() 返回头信息。
getResponseHeader() 返回指定的头信息。
open(method,url,async,uname,pswd) 规定请求的类型,URL,请求是否应该进行异步处理,以及请求的其他可选属性。

method:请求的类型:GET 或 POST
url:文件在服务器上的位置
async:true(异步)或 false(同步)
send(string) 发送请求到服务器。

string:仅用于 POST 请求
setRequestHeader() 把标签/值对添加到要发送的头文件。

XMLHttpRequest Object Properties

属性 描述
onreadystatechange 存储函数(或函数的名称)在每次 readyState 属性变化时被自动调用。
readyState 存放了 XMLHttpRequest 的状态。从 0 到 4 变化:
0:请求未初始化
1:服务器建立连接
2:收到的请求
3:处理请求
4:请求完成和响应准备就绪
responseText 返回作为一个字符串的响应数据。
responseXML 返回作为 XML 数据响应数据。
status 返回状态数(例如 "404" 为 "Not Found" 或 "200" 为 "OK")。
statusText 返回状态文本(如 "Not Found" 或 "OK")。