gpt4 book ai didi

php - AngularJS ng-repeat 不显示表中的数据

转载 作者:行者123 更新时间:2023-11-29 06:55:59 26 4
gpt4 key购买 nike

我的 MySQL 输出有问题

getCustomers.php

$query="select distinct c.ancestry, c.trusted from members c order by c.id";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$arr = array();
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$arr[] = json_encode($row);
}
}

# JSON-encode the response
$json_response = json_encode($arr);
// # Return the response
echo $json_response;

Controller 代码:

app.controller('customersCrtl', function ($scope, $http, $timeout) {
$http.get('ajax/getCustomers.php').success(function(data){
$scope.list = data;
$scope.currentPage = 1; //current page
$scope.entryLimit = 100; //max no of items to display in a page
$scope.filteredItems = $scope.list.length; //Initially for no filter
$scope.totalItems = $scope.list.length;
});
$scope.setPage = function(pageNo) {
$scope.currentPage = pageNo;
};
$scope.filter = function() {
$timeout(function() {
$scope.filteredItems = $scope.filtered.length;
}, 10);
};
$scope.sort_by = function(predicate) {
$scope.predicate = predicate;
$scope.reverse = !$scope.reverse;
};
});

问题是我从 MySQL 获得了这种格式(例如):

["{\"ancestry\":\"12865794218\",\"trusted\":\"128\"}"]

但预期的是:

[{"ancestry":"1286794218","trusted":"126"}]

所以,如果我在数据中写入常量,它就可以正常工作

$scope.list = [{"ancestry":"1286794218","trusted":"126"}];

感谢您的帮助。

最佳答案

您正在对您的回复进行双重编码。之所以存在反斜杠,是因为 json_encode 的第二个应用程序转义了第一个应用程序输出中的双引号。从 while 循环中删除 json_encode

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$arr[] = $row; // don't encode here
}
}

然后只需对其进行一次编码(就像您已经做的那样。)

$json_response = json_encode($arr);

关于php - AngularJS ng-repeat 不显示表中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45601728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com