ajax原理和XmlHttpRequest对象

function CreateXmlHttp() {

//非IE浏览器创建XmlHttpRequest对象

if (window.XmlHttpRequest) {

xmlhttp = new XmlHttpRequest();

}

//IE浏览器创建XmlHttpRequest对象

if (window.ActiveXObject) {

try {

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

}

catch (e) {

try {

xmlhttp = new ActiveXObject("msxml2.XMLHTTP");

}

catch (ex) { }

}

}

}

function Ustbwuyi() {

var data = document.getElementById("username").value;

CreateXmlHttp();

if (!xmlhttp) {

alert("创建xmlhttp对象异常!");

return false;

}

xmlhttp.open("POST", url, false);

xmlhttp.onreadystatechange = function () {

if (xmlhttp.readyState == 4) {

document.getElementById("user1").innerHTML = "数据正在加载...";

if (xmlhttp.status == 200) {

document.write(xmlhttp.responseText);

}

}

}

xmlhttp.send();

}

posted @ 2016-12-16 17:06  爱笑的3  阅读(248)  评论(0编辑  收藏  举报