[英]json response showing SyntaxError: Unexpected token [ in JSON at position 23 at JSON.parse (<anonymous>)
<?php
$db = mysqli_connect('localhost','root','','dbname')
or die('Error connecting to MySQL server.');
$query = "SELECT * FROM tablename";
$result = mysqli_query($db, $query);
header('Content-Type: applicatio`enter code here`n/json');
while($row = mysqli_fetch_row($result)){
echo json_encode($row);
}
?>
<script>
var app = angular.module('mainApp', []);
app.controller('ctrl', function($scope, $http) {
$http.get("file.php").then(function (response) {
var d = JSON.parse(response);
console.log(d);
$scope.answers = response.data.records;
});
});
</script>
Response :- ["1","santosh","1","9"]["2","chandan","2","9"] but in angularjs showing error SyntaxError: Unexpected token [ 响应:-[“ 1”,“ santosh”,“ 1”,“ 9”] [“ 2”,“ chandan”,“ 2”,“ 9”],但是在angularjs中显示错误SyntaxError:意外令牌[
you need to collect your database results in one array, then encode it into json 您需要将数据库结果收集到一个数组中,然后将其编码为json
$data = array();
while($row = mysqli_fetch_row($result)){
$data[] = $row;
}
echo json_encode($data);
and in your context , you may not need to directly use mysqli_fetch_all
which it's returns : 并且在您的上下文中,您可能不需要直接使用它返回的
mysqli_fetch_all
:
Returns an array of associative or numeric arrays holding result rows.
返回保存结果行的关联或数字数组。
as follows: 如下:
$rows = mysqli_fetch_all($result);
echo json_encode($rows);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.