[英]Unexpected token, in JSON at position 23
Angular和HTML代碼:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<p>Today's welcome message is:</p>
<h1>{{ myWelcome }}</h1>
</div>
<p>The $http service requests a page on the server, and the response is set as the value of the "myWelcome" variable.</p>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http({
method: "GET",
url: "http://localhost/dustbin/uxo_data/leaderboard.php"
}).then(function mySucces(response) {
$scope.myWelcome = response.data;
}, function myError(response) {
$scope.myWelcome = response.statusText;
});
});
</script>
</body>
</html>
PHP返回:
{"total":"4","phn":"1"},{"total":"1","phn":"2"}
錯誤:
angular.min.js:107 SyntaxError: Unexpected token , in JSON at position 23
上面代碼中的錯誤是什么,我在前端使用angular,在后端使用php作為rest-api
您的JSON字符串有效,但JSON數據不正確。
您需要將數據包裝在方括號[]
。
[{"total":"4","phn":"1"},{"total":"1","phn":"2"}]
我想您要在JSON中轉換每個對象,並在服務器端用逗號分隔發送它們。
而是在PHP中創建一個數組,其中將包含所有對象,然后使用內置的json_encode
函數獲取json 。
$jsonstr = json_encode($arr);
一條評論說您缺少[]
。 如果php發送json數據,則應該json_encode()
。
echo json_encode($object);
exit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.