页面返回xml,如何解析

来源:互联网 发布:最大公约数 c语言 编辑:程序博客网 时间:2024/06/03 16:37
<?php
class xmlController extends baseController{
 public function xmlAction(){
 $this->smarty->display('xml.tpl');
 }
 public function xmlsomeAction(){
header("Content-type:text/xml;charset=utf-8");
header("Cache-Control:no-cache");
$str= $_POST['username'];
if($str=='zhangsan'){
echo '<user><res>用户名存在</res></user>';
}else{
echo '<user><res>用户名不存在</res></user>';
}
 }

}

<script>
function fun(username){
var xhr;
if(window.ActiveObject){
xhr=new ActiveObject('Microsoft.XMLHTTP');
}else if(window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}
var url='index.php?c=xml&a=xmlsome';
xhr.open('post',url,true);
xhr.onreadystatechange=callback;
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send('username='+username);
function callback(){
if(xhr.readyState==4){
if(xhr.status==200){
info=xhr.responseXML.getElementsByTagName('user')[0].getElementsByTagName('res')[0].childNodes[0].nodeValue;
alert(info);
}
}
}
}
</script>
<form action='#' method='post'>
用户名:<input type="text" id="username" onblur='fun(this.value)'><br>
<input type="submit" value="提交">
</form>

原创粉丝点击