1)说明
XmlHttp提供了网页加载后与服务器进行通信的方法。XmlHttp最大的用处是可以更新网页的部分内容而不需要刷新整个页面。
2)创建
IE: httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
非IE: httpRequest = new XMLHttpRequest();
3)get请求例子
var xmlhttp;
function loadXMLDoc(url) {
xmlhttp = null;
if (window.XMLHttpRequest) {// code for all new browsers
xmlhttp = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{// code for IE5 and IE6
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp != null) {
xmlhttp.onreadystatechange = state_Change;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
} else {
alert("Your browser does not support XMLHTTP.");
}
console.log("the xml has been loaded");
}
function state_Change() {
if (xmlhttp.readyState == 4) {// 4 = "loaded"
if (xmlhttp.status == 200) {// 200 = OK
var data = xmlhttp.responseXML.getElementsByTagName_r("welcome-file")[0].firstChild.data;
processData(data);
// ...our code here...
} else {
alert("Problem retrieving XML data");
}
}
}
function processData(data){
console.log("Data is : ",data);
var text= document_createTextNode(data);
var p = document.getElementByIdx_x("p1");
p.a(text);
}