简体   繁体   English

意外令牌,在JSON中的位置23

[英]Unexpected token, in JSON at position 23

Angular & HTML Code : 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 Returns : PHP返回:

{"total":"4","phn":"1"},{"total":"1","phn":"2"}

Error : 错误:

angular.min.js:107 SyntaxError: Unexpected token , in JSON at position 23

Whats the error in above code, i am using angular in frontend and php in backend as rest-api 上面代码中的错误是什么,我在前端使用angular,在后端使用php作为rest-api

Your JSON String is valid but JSON Data is not accurate. 您的JSON字符串有效,但JSON数据不正确。

You need to wrap your data in square brackets [] . 您需要将数据包装在方括号[]

[{"total":"4","phn":"1"},{"total":"1","phn":"2"}]

I suppose you're converting each object in JSON and sending them separated by comma on server side. 我想您要在JSON中转换每个对象,并在服务器端用逗号分隔发送它们。

Instead, create an array in PHP which will consist of all your objects and then get json using build-in json_encode function. 而是在PHP中创建一个数组,其中将包含所有对象,然后使用内置的json_encode函数获取json

$jsonstr = json_encode($arr);

As a comment says you are missing a [] . 一条评论说您缺少[] If php sending json data then you should json_encode() it. 如果php发送json数据,则应该json_encode()

echo json_encode($object);
exit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM