Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML DOM - HttpRequest object


May 27, 2021 XML DOM


Table of contents


XMLHttpRequest object


The XMLHttpRequest object allows you to update a section of a page without reloading the entire page.


XML DOM - HttpRequest object

Try it - instance

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

Retrieving header information via getAllResponseHeaders().
Retrieving the header information for the resource (file).

The specified header information is retrieved via getResponseHeader().
Retrieves the specified header information for the resource (file).

Retrieve the contents of the ASP file
How a Web page communicates with a Web server when a user types characters in an input field.

Retrieving content from the database
How a Web page extracts information from a database through the XMLHttpRequest object.

Retrieve the contents of the XML file
Create an XMLHttpRequest to retrieve data from an 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.

The XMLHttpRequest object is a developer's dream because you can:

  • Update the page without reloading it
  • Request data from the server after the page has been loaded
  • Receive data from the server after the page has been loaded
  • Send data to the server in the background

XMLHttpRequest object method

方法 描述
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")。