xmlhttp.readyState没有改变

时间:2014-12-29 18:04:45

标签: javascript php html ajax xmlhttprequest

我正在尝试构建一个演示使用ajax的应用程序。因为我是ajax的新手我无法找到我的代码的错误。 xmlhttp对象正在创建,其余的东西都不起作用, 就绪状态未更改为1或更多,我已尝试打印所有状态值。

<html>
<head>
<title>Home</title>
<script type="text/JavaScript"> 

 function process()
 {
  var xmlhttp;
    if (window.XMLHttpRequest)
    {

        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

  if (xmlhttp.readyState==4 && xmlhttp.readyState==0)
    {
        food= document.getElementById("username").value;
        xmlhttp.open("GET","food.php?food="+food,true);
        xmlhttp.onreadystatechange=handleServerResponse();
        xmlhttp.send();     
    }
    else
    {
        setTimeout("process()",1000);
    }
}

响应功能,

function handleServerResponse()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {   
                        xmlResponse=xmlhttp.ResponseXML;

                        xmlDocumentElement= xmlResponse.documentElement;

                        message=xmlDocumentElement.firstChild.data;

                        document.getElementById("status").innerHTML="message";

            setTimeout("process()",1000);

        }
}

</script>
</head>

<body >
<form  method="post">
<fieldset><center><h2>Login</h2></center>
    <label>Username</label>
        <input type="text" id="username"  value="" maxlength="20" /> 
    <div id="status" ></div>
        <input type="button" value=" Login " onClick="process()" />
</fieldset>
</form>
</body>
</html>

php代码在

之下
<?php
echo "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>";
echo "<response";
$food=$_GET['food'];
if($food=="ajith")
  echo "Successs";
else
   echo "Invalid";
echo "</response>";   


?>

1 个答案:

答案 0 :(得分:0)

var不是可选的,请使用它。

首先var xmlhttp;是一个局部变量,因此您无法在其他方法中使用它。需要移动到过程功能之外。

你的检查是不可能的

xmlhttp.readyState==4 && xmlhttp.readyState==0

一次有两个值怎么样?

xmlhttp.readyState==4 || xmlhttp.readyState==0

下一个问题是你没有分配你正在调用它的事件处理程序

xmlhttp.onreadystatechange=handleServerResponse();

需要

xmlhttp.onreadystatechange=handleServerResponse;

没有ResponseXML,有responseXML


"message"是一个字符串,而不是您事先定义的变量。


var xmlhttp;
function process () {
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (xmlhttp) {
        var food= document.getElementById("username").value;
        xmlhttp.open("GET","food.php?food="+food,true);
        xmlhttp.onreadystatechange=handleServerResponse;
        xmlhttp.send();     
    } else {
        //alert("Can not make Ajax request");
    }
}

function handleServerResponse () {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {   
        var xmlResponse=xmlhttp.responseXML;
        var xmlDocumentElement= xmlResponse.documentElement;
        var message=xmlDocumentElement.firstChild.data;
        document.getElementById("status").innerHTML = message;
        window.setTimeout(process,1000);
    }
}