使用Jquery Ajax從Mysql中檢索數(shù)據(jù)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獲取記錄的文件。在數(shù)據(jù)庫中只有兩個字段:“名稱”,“地址”。<?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>";
} ?>此代碼無效。
3 回答

aluckdog
TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個贊
您不能返回ajax返回值。存儲全局變量存儲返回后的返回值。
或者像這樣更改你的代碼。
AjaxGet = function (url) { var result = $.ajax({ type: "POST", url: url, param: '{}', contentType: "application/json; charset=utf-8", dataType: "json", async: false, success: function (data) { // nothing needed here } }) .responseText ; return result;}
添加回答
舉報(bào)
0/150
提交
取消