JQuery GET请求将无法从php echo获取正确的数据

我试图使用jquery .get请求填充表,该URL是一个php文件。 php从数据库中获取数据,然后它应该将json数组返回到jquery数组,该数组将填充表。 虽然返回数组的长度,但表格单元格为空。

这是我的get.php函数:

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 = $(""); 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); }); } }); 

这是我的网页看起来如何,显示的是php文件响应。 在此处输入图像描述

既然反应还可以,那我的错误是细胞没有被填满? 谢谢。

如果查看JSON数据,可以看到没有teamsplayedgames等密钥。 这是因为你在PHP中使用了fetch_row() 。 将其更改为fetch_assoc()

 while ($row = $result->fetch_assoc()) 

这将为$row提供字段名称作为键,而不是使用fetch_row()提供的数字键。

你可以把php json变成javascript对象

  obj = JSON.parse(json); 

ES:

 var json='{"ex1":"test","ex2":[{"sub1":"test"},{"sub2":""s2test}],"ex3":true}'; var obj = JSON.parse(json); 

您可以使用以下方式访问数据:

 obj.ex1 // test obj.ex2[0].sub2 //s2test