使用Jquery Ajax从Mysql中检索数据

使用Jquery Ajax从Mysql中检索数据

list.php:一个简单的ajax代码,我只想显示Mysql表的记录:

<html><head>
    <script src="jquery-1.9.1.min.js">
    </script>
    <script>
    $(document).ready(function() {
        var response = '';
        $.ajax({
            type: "GET",
            url: "Records.php",
            async: false,
            success: function(text) {
                response = text;
            }
        });

        alert(response);
    });
    </script></head><body>
    <div id="div1">
        <h2>Let jQuery AJAX Change This Text</h2>
    </div>
    <button>Get Records</button></body></html>

Records.php是从Mysql获取记录的文件。
在数据库中只有两个字段:“名称”,“地址”。

<?php    //database name = "simple_ajax"
    //table name = "users"
    $con = mysql_connect("localhost","root","");
    $dbs = mysql_select_db("simple_ajax",$con);
    $result= mysql_query("select * from users");
    $array = mysql_fetch_row($result);?><tr>
    <td>Name: </td>
    <td>Address: </td></tr><?php    while ($row = mysql_fetch_array($result))
    {
        echo "<tr>";
        echo "<td>$row[1]</td>";
        echo "<td>$row[2]</td>";
        echo "</tr>";
    }   ?>

此代码无效。


忽然笑
浏览 617回答 3
3回答

aluckdog

您不能返回ajax返回值。存储全局变量存储返回后的返回值。或者像这样更改你的代码。AjaxGet&nbsp;=&nbsp;function&nbsp;(url)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;result&nbsp;=&nbsp;$.ajax({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type:&nbsp;"POST", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url:&nbsp;url, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;param:&nbsp;'{}', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;contentType:&nbsp;"application/json;&nbsp;charset=utf-8", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataType:&nbsp;"json", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;async:&nbsp;false, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;success:&nbsp;function&nbsp;(data)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;nothing&nbsp;needed&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;})&nbsp;.responseText&nbsp;; &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&nbsp;result;}
打开App,查看更多内容
随时随地看视频慕课网APP