Ajax php xmlHttp.responseXML傳回值為null

來源:互聯網
上載者:User
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 視窗中暴露無遺
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.