Php if(xmlHttp.readyState==4)为什么出错
我之前问了一个关于Php if(xmlHttp.readyState==4)为什么出错,php,javascript,ajax,Php,Javascript,Ajax,我之前问了一个关于xmlHttp.send()不起作用的代码的问题。我以为我已经把它全部修好了,但现在我又遇到了另一个问题 在handleServerResponse()函数中,代码在if(xmlHttp.readyState==4)和if(xmlHttp.readyState==200)中出错。它为什么这样做?JavaScript下有一个php代码示例 var xmlHttp = createXmlHttpRequestObject(); function createXml
xmlHttp.send()
不起作用的代码的问题。我以为我已经把它全部修好了,但现在我又遇到了另一个问题
在handleServerResponse()
函数中,代码在if(xmlHttp.readyState==4)和if(xmlHttp.readyState==200)
中出错。它为什么这样做?JavaScript下有一个php代码示例
var xmlHttp = createXmlHttpRequestObject();
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("cant create that object hos");
}else{
return xmlHttp;
}
}
function newuser() {
if (xmlHttp.readyState == 0 || xmlHttp.readyState == 4) {
name = encodeURIComponent(document.getElementById("name").value);
queryString = "name=" + name;
xmlHttp.open("GET", "code/php/core.php?" + queryString, true);
xmlHttp.onreadystatechange = handleServerRespons;
xmlHttp.send();
}else{
setTimeout('newuser()', 1000)
}
}
function handleServerRespons(){
if (xmlHttp.readyState == 4){
if (xmlHttp.readyState == 200){
alert('1234545');
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement=xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.data;
alert(message);
}
}
}
php代码:
$name = $_GET['name'];
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
echo $name;
echo '</response>';
$name=$\u GET['name'];
标题('Content-Type:text/xml');
回声';
回声';
echo$name;
回声';
xmlHttp。readyState
不能为200。您应该使用xmlHttp.status
xmlHttp.readyState
不能为200。您应该使用xmlHttp.status
xmlHttp.readyState
具有XMLHttpRequest的状态
xmlHttp.status
在xmlHttp.readyState
为3或4时可用。可用时,xmlHttp.status
应表示HTTP状态代码。xmlHttp.readyState
具有XMLHttpRequest的状态
xmlHttp.status
在xmlHttp.readyState
为3或4时可用。当可用时,xmlHttp.status
应表示HTTP状态代码。必须在onreadystatechange
事件回调中使用此
,而不是使用变量(xmlHttp
)
因此,您的功能将是:
function handleServerRespons() {
if ( this.readyState === 4 && this.status === 200 ) { // and also use "status" here not "readyState"
xmlResponse = this.responseXML;
xmlDocumentElement=xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.data;
alert( message );
}
}
或者用(function(){…})()包装代码代码>如下所示
(function() {
// all your code goes here, so you can use that 'xmlHttp' instead of 'this'
})();
您必须在onreadystatechange
事件回调中使用this
,而不是使用变量(xmlHttp
)
因此,您的功能将是:
function handleServerRespons() {
if ( this.readyState === 4 && this.status === 200 ) { // and also use "status" here not "readyState"
xmlResponse = this.responseXML;
xmlDocumentElement=xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.data;
alert( message );
}
}
或者用(function(){…})()包装代码代码>如下所示
(function() {
// all your code goes here, so you can use that 'xmlHttp' instead of 'this'
})();
xmlHttp对象是如何创建的,您介意发布吗?另外,readyState
将永远不会是200。是否要使用.status
属性?如果(xmlHttp.status?==200){?还得到错误:无法读取documentElement。有解决方案吗?xmlHttp对象是如何创建的,您介意发布吗?另外,readyState
将永远不会是200。您的意思是使用.status
属性吗?如果(xmlHttp.status?==200){还得到错误:无法读取documentElement。有解决方案吗?'if(xmlHttp.readyState==4){if(xmlHttp.status==200){像那样?@MattisBratland Yes;xmlHttp.status是状态码,如果页面加载正确,则状态码应该是200(例如,如果没有找到,则为404)。'if(xmlHttp.readyState==4){if(xmlHttp.status==200){`像那样?@MattisBratland是的;xmlHttp.status是状态码,如果页面加载正确,则状态码应该是200(例如,如果找不到,则状态码应该是404)。