php コード
<?php header('Content-Type:text/xml'); echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'; echo'<response>'; $name=$_GET['name']; $userName=array('WangWei','ZhouJianfei','MeiShibo','QuXinglin','WangYuming','LiaoGuihong','WangChenggao','ZhouQian'); if(in_array(strtoupper($name),$userName)){ echo 'Hello,master'.htmlentities($name).'!'; }else if(trim($name)==''){ echo 'Stranger,please tell me your name!'; }else{ echo htmlentities($name).',I don\'t know you!'; } echo '</response>';?>
var xmlHttp=createXmlHttpRequestObject(); //get xmlHttpRequest object function createXmlHttpRequestObject(){ var xmlHttp; if(window.ActiveXObject){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){ xmlHttp=false; } }else{ try{ xmlHttp=new XMLHttpRequest(); } catch(e){ xmlHttp=false; } } if(!xmlHttp){ alert("Error creating the XMLHttpRequest object!"); }else{ return xmlHttp; } } function process(){ if(xmlHttp.readyState==4||xmlHttp.readyState==0){ name=encodeURIComponent(document.getElementById("myName").value); xmlHttp.open("GET","quickstart.php?name="+name,true); xmlHttp.onreadystatechange=handleServerResponse; xmlHttp.send(null); }else{ setTimeout('process()',1000); } } function handleServerResponse(){ if(xmlHttp.readyState==4){ if(xmlHttp.status==200){ xmlResponse=xmlHttp.responseXML; alert(xmlHttp.responseXML); xmlDocumentElement=xmlResponse.documentElement; helloMessage=xmlDocumentElement.firstChild.data; document.getElementById('divMessage').innerHTML='<i>'+helloMessage+'</i>'; setTimeout('process()',1000); }else{ alert('There was a problem accessing hte server:'+xmlHttp.statusText); } } }
alter(xmlHttp.responseXML)
この書き方は無責任です
すべてがうまくいけば、それは DOMDocument オブジェクトです。
したがって、
xmlResponse = xmlHttp.responseXML;if(xmlResponse.xml == '') { alert(xmlHttp.responseText); return;}