简体   繁体   English

显示SyntaxError的json响应:意外令牌[JSON中JSON.parse( <anonymous> )

[英]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.

相关问题 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) SyntaxError:JSON中的意外令牌&lt;位于JSON.parse(位置0) <anonymous> )-AngularJS - SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) - AngularJS 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse的位置2 <anonymous> )在对象上。 <anonymous> (userhistory.js:23) - Uncaught SyntaxError: Unexpected token < in JSON at position 2 at JSON.parse (<anonymous>) at Object.<anonymous> (userhistory.js:23) Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0 at JSON.parse (<anonymous> ) 在 XMLHttpRequest。<anonymous></anonymous></anonymous> - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.<anonymous> Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0 : at JSON.parse (<anonymous> ) 在对象。<anonymous> - Uncaught SyntaxError: Unexpected token < in JSON at position 0 : at JSON.parse (<anonymous>) at Object.<anonymous> jQuery.Deferred exception: Unexpected token &lt; in JSON at position 0 SyntaxError: Unexpected token &lt; in JSON at position 0 at JSON.parse (<anonymous> )</anonymous> - jQuery.Deferred exception: Unexpected token < in JSON at position 0 SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) 我正在使用Ionic并收到错误消息:SyntaxError:JSON中的意外标记&lt;在JSON.parse位置0处( <anonymous> ) - I am using ionic and getting an error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous> ) - VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) 未捕获的语法错误:JSON 中的意外令牌 < position 中的 0 JSON.parse - Wordpress - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse - Wordpress JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM