XMLHttpRequest和ActiveXObject学习

当前位置 : 首页 > 网页制作 > html5 > XMLHttpRequest和ActiveXObject学习

XMLHttpRequest和ActiveXObject学习

来源: 作者: 时间:2016-02-01 11:22
var xmlHttpReq = new ActiveXObject( MSXML2 XMLHTTP 3 0 ); 定义变量,存储对象 var xmlHttp; 创建XMLHttpRequest对象if (window ActiveXObject) { xmlHttpReq = new Activ
//var xmlHttpReq = new ActiveXObject("MSXML2.XMLHTTP.3.0");

//定义变量,存储对象
var xmlHttp;
// 创建XMLHttpRequest对象
if (window.ActiveXObject) {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();
}

xmlHttpReq.open("GET", "http://localhost:9090/5/test", false);
xmlHttpReq.onreadystatechange = handleStateChange;
xmlHttpReq.send();

// xmlHttpReq.responseText
// xmlHttpReq.responseXML

var htmlObj = "";
htmlObj += "=============状态码=================
";
htmlObj += "status=" + xmlHttpReq.status + "
";
htmlObj += "statusText=" + xmlHttpReq.statusText + "
";
htmlObj += "=============头信息=================
";
htmlObj += "heads=" + xmlHttpReq.getAllResponseHeaders() + "
";
htmlObj += "Content-Length=" + xmlHttpReq.getResponseHeader("Content-Length") + "
";
htmlObj += "=============返回信息=================
";
htmlObj += "responseStream=" + xmlHttpReq.responseStream + "
";

var obj = document.getElementById("showDiv");
obj.innerHTML = htmlObj;

function handleStateChange() {
// 请求的状态有5个值:0=未初始化;1=正在加载;2=已经加载;3=交互中;4=完成;
if (xmlHttpReq.readyState == 4) {
// 200对应OK,如404=未找到网页
if (xmlHttpReq.status == 200) {
// alert(xmlHttpReq.responseText);
}
}
}
Tag:
网友评论

<