创建一个XMLRequest对象:xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
1、成功初始化一个XMLRequest对象时,xmlHttp的内容:
window.alert("xmlHttp = "+xmlHttp);
如图所示:
2、成功初始化一个XMLRequest对象时,xmlHttp.readyState的内容:
3、发送请求,执行了回调函数showMessage(),一进入回调函数中:
function showMessage(){
window.alert("xmlHttp.getAllResponseHeaders() = "+xmlHttp.getAllResponseHeaders());
if(xmlHttp.readyState == 4){
if(xmlHttp.status==200){
var response = xmlHttp.responseText.split("|");
document.getElementById("name_msg").innerHTML = response[0];
document.getElementById("pwd_msg").innerHTML = response[1];
document.getElementById("email_msg").innerHTML = response[2];
}
}
}
或者:发送请求,执行了回调函数showMessage(),进入回调函数中,当(xmlHttp.readyState == 4)且(xmlHttp.status==200):
function showMessage(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status==200){
window.alert("xmlHttp.getAllResponseHeaders() = "+xmlHttp.getAllResponseHeaders());
var response = xmlHttp.responseText.split("|");
document.getElementById("name_msg").innerHTML = response[0];
document.getElementById("pwd_msg").innerHTML = response[1];
document.getElementById("email_msg").innerHTML = response[2];
}
}
}
则xmlHttp.getAllResponseHeaders()的内容是一样的。
如图所示:
4、发送请求,执行了回调函数showMessage(),进入回调函数中,当(xmlHttp.readyState == 4)且(xmlHttp.status==200),则xmlHttp.responseText:
function showMessage(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status==200){
window.alert("xmlHttp.responseText = "+xmlHttp.responseText);
var response = xmlHttp.responseText.split("|");
document.getElementById("name_msg").innerHTML = response[0];
document.getElementById("pwd_msg").innerHTML = response[1];
document.getElementById("email_msg").innerHTML = response[2];
}
}
}
如图所示:
5、发送请求,执行了回调函数showMessage(),进入回调函数中,当(xmlHttp.readyState == 4)且(xmlHttp.status==200),则xmlHttp.statusText:
function showMessage(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status==200){
window.alert("xmlHttp.statusText = "+xmlHttp.statusText);
var response = xmlHttp.responseText.split("|");
document.getElementById("name_msg").innerHTML = response[0];
document.getElementById("pwd_msg").innerHTML = response[1];
document.getElementById("email_msg").innerHTML = response[2];
}
}
}
如图所示:
6、新建一个XML文件user.xml,内容为:
<?xml version='1.0' encoding='UTF-8' ?>
<user>
<name>异域王者</name>
<pwd>830119</pwd>
<email>shirdrn@hotmail.com</email>
</user>
发送请求,执行了回调函数showMessage(),进入回调函数中,当(xmlHttp.readyState == 4)且(xmlHttp.status==200),则xmlHttp.responseXML.xml:
xmlHttp.open("GET","user.xml",true); // 这里,打开的URL为user.xml文件URL
xmlHttp.onreadystatechange = showMessage;
xmlHttp.send(null);
}
function showMessage(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status==200){
window.alert("xmlHttp.responseXML.xml = "+xmlHttp.responseXML.xml);
var response = xmlHttp.responseText.split("|");
document.getElementById("name_msg").innerHTML = response[0];
document.getElementById("pwd_msg").innerHTML = response[1];
document.getElementById("email_msg").innerHTML = response[2];
}
}
}
如图所示:
关于对XML文本的操作,是Ajax中最具有潜力的。