XMLHttpRequest和ActiveXObject學習

zongxiao08發表於2014-02-08
//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", "", 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);
        }
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28519454/viewspace-1077838/,如需轉載,請註明出處,否則將追究法律責任。

相關文章