【问题标题】:Ajax Returns Random Values?Ajax 返回随机值?
【发布时间】:2009-06-06 01:45:05
【问题描述】:

我正在开发一个简单的 AJAX 页面。当页面加载时,它应该从 PHP 页面获取结果并将其显示在文本框中。如果结果是“1”(应该是),那么它应该会弹出一个提示“Ready”。

主页代码(t1_wait.php):

<html><head><title>Waiting...</title></head><body>

<script type="text/javascript">
function update(id)
{
   var xmlhttp;
   if (window.XMLHttpRequest){
         // code for IE7+, Firefox, Chrome, Opera, Safari
         xmlhttp=new XMLHttpRequest();
   }else if (window.ActiveXObject){
      // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }else{
      alert("Your browser does not support XMLHTTP!");
   }

   xmlhttp.onreadystatechange=function(){
      if(xmlhttp.readyState==4){
         if(xmlhttp.responseText=="1")
            alert("Ready!");
         }
         document.myForm.status.value=xmlhttp.responseText;
      }
   }

   var requesturl = "t1_checkMatch.php?id="+id;
   xmlhttp.open("GET",requesturl,true);
   xmlhttp.send(null);

   // delay for 1 sec
   var date = new Date();
   var curDate = null;
   do { curDate = new Date(); }
   while(curDate-date < 1000);

}

<?php
   echo "update(".$_GET['id'].");";
?>

</script>


<form name="myForm">
Status: <input type="text" name="status" />
</form>

</body></html>

调用 (t1_checkMatch.php) 的 PHP 页面(所有 db 信息替换为 *****):

<?php
$db_user = "*****";
$db_pass = "*****";
$db_name = "*****";
mysql_connect(localhost,$db_user,$db_pass);
@mysql_select_db($db_name) or die("Unable to select database");

$match_id = $_GET['id'];

$match_info = mysql_query("SELECT * FROM ***** WHERE id=".$match_id);
if(mysql_result($match_info,0,"usr2")==-1){
   echo "1";
}else{
   echo "0";
}
?>

当我转到 t1_wait.php?id=16(通过 GET 传递 id=16 的主页)时,它应该向 t1_checkMatch.php?id=16 发送请求,它返回(是的,我检查了)1 . 这应该会触发一个提示“Ready”并导致 1 出现在文本框中,但是这些事情都没有发生。文本框为空白。

怎么了?谢谢!

【问题讨论】:

  • 为什么又要发明轮子?使用其中一个库(例如 mootools),您的整个 JS 代码最多会减少到 1-5 行。
  • 然后他会为 mootools 添加几百个 :)

标签: php javascript ajax


【解决方案1】:

我认为您遇到的问题是由于打字错误

xmlhttp.responceText

真的应该是

xmlhttp.responseText

-- 更新

您似乎还缺少{

if(xmlhttp.responseText=="1")
   alert("Ready!");
}

应该是

if(xmlhttp.responseText=="1"){
   alert("Ready!");
}

【讨论】:

    【解决方案2】:

    你有一个拼写错误:

    if(xmlhttp.responceText=="1")
    

    应该是:

    if(xmlhttp.responseText=="1")
    

    (您的响应拼写错误)

    【讨论】:

      【解决方案3】:

      好的。我想通了,但我不知道我做了什么。我确实有错字,但这不是问题。 PHP代码是一样的,这里是主页面代码:

      <html>
      <body>
      
      <script language="javascript" type="text/javascript">
      <!-- 
      //Browser Support Code
      function update(id){
          var ajaxRequest;  // The variable that makes Ajax possible!
      
          try{
              // Opera 8.0+, Firefox, Safari
              ajaxRequest = new XMLHttpRequest();
          } catch (e){
              // Internet Explorer Browsers
              try{
                  ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
              } catch (e) {
                  try{
                      ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                  } catch (e){
                      // Something went wrong
                      alert("Your browser broke!");
                      return false;
                  }
              }
          }
          // Create a function that will receive data sent from the server
          ajaxRequest.onreadystatechange = function(){
              if(ajaxRequest.readyState == 4){
                  if(ajaxRequest.responseText.indexOf("1")!=-1){
                     document.myForm.status.value = "Ready!";
                     window.location = "t1_game.php?id="+id;
                  }else{
                     document.myForm.status.value = "Waiting..."
                     update(id);
                  }
              }
          }
          ajaxRequest.open("GET", "t1_checkMatch.php?id="+id, true);
          ajaxRequest.send(null); 
      }
      
      <?php
      echo "update(".$_GET["id"].");"
      ?>
      
      //-->
      </script>
      
      
      
      <form name='myForm'>
      Status: <input type='text' name='status' />
      </form>
      </body>
      </html>
      

      【讨论】:

      • 解决了,我只是想把它放在那里以防其他人有同样的问题
      猜你喜欢
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 2019-04-26
      相关资源
      最近更新 更多