2017-03-07 52 views
0

角& HTML代碼:意外標記,在JSON在位置23

<!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

+2

返回的數據是無效的JSON。至少一個周圍的'[]'會丟失。 – Sirko

+0

在php中使用'json_encode'來返回數組。 –

+0

@sachilaranawaka你可以添加相關的PHP代碼嗎?至少你使用'json_encode()'錯誤的方式。 – Sirko

回答

0

您的JSON字符串有效,但JSON數據不準確。

您需要將數據包裝在方括號內[]

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

我想你是轉換JSON中的每個對象,併發送它們在服務器端用逗號分隔。

相反,在PHP中創建一個數組,將包括你所有的物體,然後使用JSON得到積聚在json_encode function.

$jsonstr = json_encode($arr); 
0

正如評論說你缺少一個[]。如果php發送json數據,那麼你應該json_encode()它。

echo json_encode($object); 
exit(); 
0

數組裏面放對象是這樣

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

也使用json_decode打印的數組,如果它的JSON數據

echo json_encode($obj); 
相關問題