关于XMLRequest对象的属性和方法

创建一个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中最具有潜力的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值