【问题标题】:Why wont this ajax script work on wamp localhost为什么这个 ajax 脚本不能在 wamp localhost 上工作
【发布时间】:2012-07-19 11:54:51
【问题描述】:

我在运行 Windows 7 家庭高级版、apache、php 和 mysql 的本地计算机上尝试了这个 ajax 示例,但它不会向浏览器返回任何结果。在这里阅读了几篇文章后,我下载了firebug,并从firebug控制台->我得到的都是这个:

GET http://localhost/dev/ajax/ajax-example.php?age=100&wpm=100&sex=m 200 OK 1.01s

回应

Query: SELECT * FROM ajax_example WHERE sex = 'm' AND age <= 100 AND wpm <= 100
<br /><table><tr><th>Name</th><th>Age</th><th>Sex</th><th>WPM</th>
</tr><tr>td>Frank</td><td>45</td><td>m</td><td>87</td></tr><tr><td>Regis</td>
<td>75</td><td>m</td><td>44</td></tr></table>

上面的这个东西应该是去浏览器的。该脚本似乎工作正常,除了 div 不会刷新新内容。

这是浏览器问题还是 windows/javascript 问题。我需要做什么才能使它正常工作?你能帮忙吗?

这是我从中获得所有这些的教程页面。

http://www.tutorialspoint.com/ajax/ajax_database.htm

这个页面是ajax.html

<html>
<body>
<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 and will update
 // div section in the same page.
 ajaxRequest.onreadystatechange = function(){
   if(ajaxRequest.readyState == 4){
      var ajaxDisplay = document.getElementById('ajaxDiv');
      ajaxDisplay.value = ajaxRequest.responseText;
   }
 }
 // Now get the value from user and pass it to
 // server script.
 var age = document.getElementById('age').value;
 var wpm = document.getElementById('wpm').value;
 var sex = document.getElementById('sex').value;
 var queryString = "?age=" + age ;
 queryString +=  "&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 value="m">m</option>
<option value="f">f</option>
</select>
<input type="button" onclick="ajaxFunction()" value="Query MySQL"/>
</form>
<div id="ajaxDiv">Your result will display here</div>
</body>
</html>

这个页面是ajax-example.php

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "norman";
$dbname = "test";
    //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 sex = '$sex'";
if(is_numeric($age))
    $query .= " AND age <= $age";
if(is_numeric($wpm))
    $query .= " AND 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[name]</td>";
    $display_string .= "<td>$row[age]</td>";
    $display_string .= "<td>$row[sex]</td>";
    $display_string .= "<td>$row[wpm]</td>";
    $display_string .= "</tr>";

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

【问题讨论】:

    标签: php javascript mysql ajax apache


    【解决方案1】:

    ajaxDisplay.value = ajaxRequest.responseText;这里是错误的,比如改成ajaxDisplay.innerHTML = ajaxRequest.responseText; 和 mb 使用 jquery 或其他框架来完成这种琐碎的任务?

    【讨论】:

    • 如果是微不足道的,为什么要使用jQuery?有点像打开核桃很容易,所以使用大锤更容易。
    • 您的建议奏效了。感谢您的帮助。它实际上是网站的错误。它的 ajaxDisplay.value = ajaxRequest.responseText;在示例中,但 ajaxDisplay.innerHTML = ajaxRequest.responseText;在他们的网站实际使用的代码中。
    • 2 epascarello yse 这很简单,但对于作者来说不是这样,使用 jquery 他可以少写 3 次代码,对于最终用户来说,等待更多 0.01 秒是没有问题的
    【解决方案2】:

    在回调函数中

    ajaxRequest.onreadystatechange = function(){
       if(ajaxRequest.readyState == 4){
          var ajaxDisplay = document.getElementById('ajaxDiv');
          ajaxDisplay.value = ajaxRequest.responseText;
       }
    }
    

    我不认为 ajaxDisplay.value 会起作用!

    改为使用

    ajaxDisplay.innerHTML = ajaxRequest.responseText;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-20
      • 1970-01-01
      • 1970-01-01
      • 2014-02-05
      • 2016-03-19
      • 2013-05-06
      相关资源
      最近更新 更多