php接收ajax值为null,Ajax php xmlHttp.responseXML返回值为null

本文探讨了PHP代码中使用XMLHttpRequest的实例,重点在于解决请求返回值为null的问题,并提供了处理服务器响应和错误检查的方法。作者强调了正确处理XMLHttpRequest响应的重要性。
摘要由CSDN通过智能技术生成

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 窗口中暴露无遗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值