php代碼
';echo'';$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 '';?>
var xmlHttp=createXmlHttpRequestObject();//get xmlHttpRequest objectfunction 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=''+helloMessage+'';setTimeout('process()',1000);}else{alert('There was a problem accessing hte server:'+xmlHttp.statusText);}}}
alert(xmlHttp.responseXML);傳回值是null 哪裡錯了?
另外我用的阿里 免費虛擬機器主機 上面的php
回複討論(解決方案)
alert(xmlHttp.responseXML)
這個寫法是不負責任的
一切順利的話,他是一個 DOMDocument 對象,用 alert 至多看到 [Object]
所以你應寫作
xmlResponse = xmlHttp.responseXML;if(xmlResponse.xml == '') { alert(xmlHttp.responseText); return;}
這樣無論是 XML 格式不對,還是 php 程式出現問題,都會在 alert 視窗中暴露無遺