【发布时间】:2018-01-17 23:53:41
【问题描述】:
我的 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;
和控制器代码:
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"}]
所以,如果我在 data 中写入常量,它工作得非常好
$scope.list = [{"ancestry":"1286794218","trusted":"126"}];
感谢您的帮助。
【问题讨论】:
-
“\”是正确的。捕获数组时 javascript 显示错误?
-
检查这个答案以澄清反斜杠。 stackoverflow.com/a/10314758/8303694