dongluan7821 2014-12-29 18:04
浏览 45

xmlhttp.readyState没有改变

i was trying to build a application that demonstrate the use of ajax. as i am a newbie in ajax i couldnt able to find the error for my code. xmlhttp object is creating, rest of the things are not working, the ready state is not changing to 1 or more than that, i have tried by printing all status values.

<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);
    }
}

response function,

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 code is below

<?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条回答 默认 最新

  • duanjieyi6582 2014-12-29 18:06
    关注

    var is not optional, use it.

    First var xmlhttp; is a local variable, so you would not be able to use it in the other method. Needs to be moved outside of the process function.

    Your if check is is impossible

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

    How can something be 2 values at once?

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

    Next issue is the fact you are not assigning an event handler you are calling it

    xmlhttp.onreadystatechange=handleServerResponse();
    

    needs to be

    xmlhttp.onreadystatechange=handleServerResponse;
    

    There is no ResponseXML, there is responseXML


    "message" is a string, not the variable you defined beforehand.


    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);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大