我正在尝试使用 jquery .get 请求填充表,url 是一个 php 文件。php 从数据库中获取数据,然后它应该将一个 json 数组返回给 jquery,该数组将填充表。虽然数组的长度按原样返回,但表格单元格为空。
这是我的 get.php 函数:
<?php
$mysqli=new mysqli("localhost:3306","root","","leagues");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$return_arr = array();
$query = "SELECT * FROM league";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_row())
{
$return_arr[] = $row;
}
}
$mysqli->close();
header('Content-Type: application/json');
echo json_encode($return_arr);
?>
这是我的 jquery get 函数
$.get('php/get.php',function(responseJson)
{
if(responseJson!=null)
{
$("#tableleague").find("tr:gt(0)").remove();
var table1 = $("#tableleague");
$.each(responseJson, function(key,value) {
var rowNew = $("<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>");
rowNew.children().eq(0).text(value['teams']);
rowNew.children().eq(1).text(value['playedgames']);
rowNew.children().eq(2).text(value['wongames']);
rowNew.children().eq(3).text(value['tiegames']);
rowNew.children().eq(4).text(value['lostgames']);
rowNew.children().eq(5).text(value['scoredgoal']);
rowNew.children().eq(6).text(value['receivedgoal']);
rowNew.children().eq(7).text(value['points']);
rowNew.appendTo(table1);
});
}
});
既然响应没问题,我做错了什么,单元格没有被填满?谢谢你。
如果您查看您的 JSON 数据,您会发现没有诸如teams
或 之类的键playedgames
。这是因为你fetch_row()
在 PHP 中使用过。将其更改为fetch_assoc()
:
while ($row = $result->fetch_assoc())
这将为您$row
提供字段名称作为键,而不是使用fetch_row()
提供的数字键。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句