在编写 Ajax 方法的时候,我们经常会写上类似于这样的代码:
Ajax 代码:
var xmlHttp;
//创建一个XmlHttpRequeset对象
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
//开始一个请求
function startRequest(){
createXMLHttpRequest();
xmlHttp.onreadystatechange = handlestatechange;
xmlHttp.open("GET", "SimpleRespose.xml", true);
xmlHttp.Send(null);
}
function handlestatechange(){
if(xmlHttp.readyState == 4){//描述一种"已加载"状态;此时,响应已经被完全接收。
if(xmlHttp.status == 200){//200表示成功收到
alert("The Server Replied with:" + xmlHttp.responseText)
}
}
}
相关阅读