首页 > 其他 > 详细

XMLHttpRequest和ActiveXObject学习

时间:2014-02-07 17:59:30      阅读:388      评论:0      收藏:0      [点我收藏+]
//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/html5/test", false);
xmlHttpReq.onreadystatechange = handleStateChange;
xmlHttpReq.send();

// xmlHttpReq.responseText
// xmlHttpReq.responseXML

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

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);
        }
    }
}

XMLHttpRequest和ActiveXObject学习

原文:http://blog.csdn.net/main_xtgjfge/article/details/18960409

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!