Home >Web Front-end >JS Tutorial >Understanding of xmlHttp object methods and properties_Basic knowledge
1.1 我的理解:
2 xmlHttp对象的成员。
2.1 属性
例子:xmlHttp.onreadystatechange = functionHandler;
function functionHandler() {
if(xmlHttp.readyState == 4) {
alert("当readyState状态为4时,弹出此窗口!!!");
}
}
//句柄只有方法名称,没有这对“()”括号。赋值时要注意理解。
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
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.
Whenadopts the asynchronous method (true), the callback function specified by the onreadystatechange attribute will be called when the state changes.
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.