Javascript 意外标记,JSON格式,位置23
角度和HTML代码:Javascript 意外标记,JSON格式,位置23,javascript,php,angularjs,json,Javascript,Php,Angularjs,Json,角度和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">
<!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>
错误:
angular.min.js:107语法错误:意外标记,JSON格式,位置23
上面代码中的错误是什么,我在前端使用angular,在后端使用php作为rest api您的JSON字符串有效,但JSON数据不准确 您需要将数据用方括号括起来
[]
[{"total":"4","phn":"1"},{"total":"1","phn":"2"}]
我假设您正在转换JSON中的每个对象,并在服务器端以逗号分隔发送它们
相反,在PHP中创建一个包含所有对象的数组,然后使用内置函数获取json
正如一条评论所说,您缺少一个
[]
。如果php发送json数据,那么您应该json\u encode()
it
echo json_encode($object);
exit();
返回的数据不是有效的JSON。至少缺少一个周围的
[]
。在php中,使用json\u encode
返回数组。@sachilaranawaka您可以添加相关的php代码吗?至少你使用了json\u encode()?
错误的方法。你能告诉php代码片段你在哪里以及如何使用json\u encode()
方法吗?
$jsonstr = json_encode($arr);
echo json_encode($object);
exit();