Home >Web Front-end >JS Tutorial >Understanding of xmlHttp object methods and properties_Basic knowledge

Understanding of xmlHttp object methods and properties_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 18:11:51959browse
1  客户端可以通过xmlHttp对象(MSXML2.XMLHTTP.3.0)向http服务器发送请求并使用文档对象模型(DOM)处理回应。

1.1 我的理解:

  1. 用户的每次操作,都会有数据产生。
  2. 通过DOM或者JS编写对数据进行封装,或者浏览器自身对http协议的一些数据进行封装。
  3. 通过xmlHttp对象的一些方法,传入数据参数,向http服务器发送请求。
  4. 返回结果通过DOM进行处理。

2  xmlHttp对象的成员。

2.1 属性

  1. onreadystatechange:readyState属性值发生改变时,触发的事件处理句柄。

例子:xmlHttp.onreadystatechange = functionHandler;

   function functionHandler() {

if(xmlHttp.readyState == 4) {

alert("readyState状态为4时,弹出此窗口!!!");

}

  }

//句柄只有方法名称,没有这对“()”括号。赋值时要注意理解。

  1. readyState:这个属性表示状态;总共有五种状态:

0 (未初始化)

对象已建立,但是尚未初始化(尚未调用open方法)

1 (初始化)

对象已建立,尚未调用send方法

2 (发送数据)

send方法已调用,但是当前的状态及http头未知

3 (数据传送中)

已接收部分数据,因为响应及http头不全,这时通过responseBody和responseText获取部分数据会出现错误,

4 (完成)

数据接收完毕,此时可以通过通过responseBody和responseText获取完整的回应数据

// Because xmlHttp is written in a fixed way, so each step will be accompanied by a change of state, so the event handler is always monitored , execute the corresponding logic.

Code execution order:

var xmlHttpReq = new ActiveXObject("MSXML2.XMLHTTP.3.0");

xmlHttpReq.open("GET", "http://localhost/test.xml", false);

xmlHttpReq.send();

alert(xmlHttpReq.responseText);

2.2 Method

  1. open(Method, Url, Syn, User, Password);

When you create a new xmlHttp object, you actually create a http request.

This method specifies the request method(GET/POST/PUT/PROPFIND), URL, asynchronous(The default is true), verification information.

When

adopts the asynchronous method (true), the callback function specified by the onreadystatechange attribute will be called when the state changes.

  1. send();

The synchronous or asynchronous mode of this method depends on the Syn parameter in the open method, if Syn = = false, this method will wait for the request to complete or timeout before returning, if Syn == true, this method will return immediately.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn