【问题标题】:xmlhttp.readyState is not changingxmlhttp.readyState 没有改变
【发布时间】:2014-12-30 02:20:03
【问题描述】:

我正在尝试构建一个演示如何使用 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>";   


?>

【问题讨论】:

    标签: javascript php html ajax xmlhttprequest


    【解决方案1】:

    var 不是可选的,使用它。

    首先var xmlhttp; 是一个局部变量,所以你不能在其他方法中使用它。需要移到流程功能之外。

    你的 if 检查是不可能的

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

    一个东西怎么可能同时是 2 个值?

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

    【讨论】:

      猜你喜欢
      • 2012-12-26
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-02
      相关资源
      最近更新 更多