为什么readyState不等于4?

时间:2013-02-18 02:16:26

标签: javascript php ajax debugging readystate

我试图学习如何使用ajax从数据库接收数据,我有一些问题......

问题是当我提交查询时我没有看到任何东西...... 我知道问题是因为readyState不等于4 ......但为什么?

我在index.html文件上有这个代码:

  

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
    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){
            document.myForm.time.value = ajaxRequest.responseText;
        }
    }
    var age = document.getElementById('age').value;
    var wpm = document.getElementById('wpm').value;
    var sex = document.getElementById('sex').value;
    var queryString = "?age=" + age + "&wpm=" + wpm + "&sex=" + sex;
    ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
    ajaxRequest.send(null); 
}

//-->
</script>



<form name='myForm'>
   Max Age: <input type='text' id='age' /> <br />
   Max WPM: <input type='text' id='wpm' />
   <br />
   Sex: <select id='sex'>
          <option>m</option>
          <option>f</option>
        </select>

    <input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</form>

和ajax-example.php文件中的代码:

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "*************";
$dbname = "*************";
    //Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
    //Select Database
mysql_select_db($dbname) or die(mysql_error());
    // Retrieve data from Query String
$age = $_GET['age'];
$sex = $_GET['sex'];
$wpm = $_GET['wpm'];
    // Escape User Input to help prevent SQL Injection
$age = mysql_real_escape_string($age);
$sex = mysql_real_escape_string($sex);
$wpm = mysql_real_escape_string($wpm);
    //build query
$query = "SELECT * FROM ajax_example WHERE ae_sex = '$sex'";
if(is_numeric($age))
    $query .= " AND ae_age <= $age";
if(is_numeric($wpm))
    $query .= " AND ae_wpm <= $wpm";
    //Execute query
$qry_result = mysql_query($query) or die(mysql_error());

    //Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Age</th>";
$display_string .= "<th>Sex</th>";
$display_string .= "<th>WPM</th>";
$display_string .= "</tr>";

    // Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
    $display_string .= "<tr>";
    $display_string .= "<td>$row[ae_name]</td>";
    $display_string .= "<td>$row[ae_age]</td>";
    $display_string .= "<td>$row[ae_sex]</td>";
    $display_string .= "<td>$row[ae_wpm]</td>";
    $display_string .= "</tr>";

}
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;
?>

3 个答案:

答案 0 :(得分:1)

操作顺序必须是:

  1. 创建AJAX对象
  2. 打开连接
  3. 设置readystatechange处理程序
  4. 发送请求。
  5. 您的项目2和3的顺序错误,因此正在清除事件处理程序,并且永远不会调用它。

    编辑:还应该注意的是,自2007年以来,随着IE7的发布,new XMLHttpRequest已完全跨浏览器。您现在不需要使用这些ActiveX对象。

答案 1 :(得分:0)

它看起来像两件事之一:

  • localhost被用作域名;它应该是127.0.0.1
  • ajax-example.php正在localhost上运行,但关联的HTML文件不是

答案 2 :(得分:0)

var queryString =“?age =”+ age +“&amp; wpm =”+ wpm +“&amp; sex =”+ sex;

  

你应该使用double和(&amp;&amp;)   试试吧

var queryString =“?age =”+ age +“&amp;&amp; wpm =”+ wpm +“&amp;&amp; sex =”+ sex;