var oDiv = document.getElementById("div1");
document.onclick = function(){
//var xhr = new XMLHttpRequest(); //创建XHR对象
var xhr;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();//创建了一个XHR对象;
}else{
xhr = new ActiveXObject("MSxml12.XMLHTTP");//兼容IE6以下
}
xhr.open("get","demo.php",true); //准备发送请求 第一个参数:get/post 第二个:url 第三个:true(异步)
// 设置回调函数
xhr.onreadystatechange = function(){
if(xhr.readyState==4){
if(xhr.status==200){
// alert(xhr.responseText);
div1.innerHTML = xhr.responseText;
}else{
alert("error,restart");
}
}
}
// 发送请求
xhr.send(null); //get方式发送请求,send参数就是null
}
//兼容:
// new AativeXObject("MSxml2.XMLHTTP") //IE6 IE5
// new XMLHttpRequest(); //其他浏览器;
//如果有就创建
var request;
if(window.XMLHttpRequest){
request = new XMLHttpRequest();//创建了一个XHR对象;
}else{
request = new ActiveXObject("MSxml12.XMLHTTP");//兼容IE6以下
}