Ⅰ、建立
1、依据不同的浏览器,取得XMLHttpRequest物件
var xmlHttp;
function createXHR () {
var xmlHttp = false;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest ();
}else if (window.ActiveXObject) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e){
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e) {
xmlHttp = false;
}
}
}
return xmlHttp;
}
Ⅱ、送出请求
function sendRequest(){
createXHR();
var url='*.*?ts='+new Date().getTime();
alert(url);
xmlHttp.open('GET',url,true);//open 方法的第一个参数是两种请求的方法,get或post
xmlHttp.onreadystatechange=catchXML;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");//若为post请求,要加上头信息
xmlHttp.send(null);/ ; //送出请求
}
Ⅲ、接收回应
Function catchXML(){
If(xmlHttp.readyState == 4){
If(xmlHttp.status == 200 || xmlHttp.status == 304){
//成功送出回应
相应得处理。
}
Else{
}
}
}