【发布时间】:2017-03-07 07:24:17
【问题描述】:
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。至少周围的
[]会丢失。 -
在php中使用
json_encode返回数组。 -
@sachilaranawaka 可以添加相关的PHP代码吗?至少你用错了
json_encode()?。 -
你能告诉 php sn-p 你在哪里以及如何使用
json_encode()方法吗?
标签: javascript php angularjs json