我一直在尝试发出 ajax 请求,但似乎有问题。当我的 json 属性名称在 " (例如 {"name":value"} )中时,它可以工作,但是当属性名称不是时。我有以下异常
SyntaxError: Unexpected token s
at Object.parse (native)
at pb (http://localhost:8080/angularjs/lib/angular.min.js:12:472)
at Vc.d.defaults.transformResponse (http://localhost:8080/angularjs/lib/angular.min.js:92:314)
at http://localhost:8080/angularjs/lib/angular.min.js:92:127
at Array.forEach (native)
at n (http://localhost:8080/angularjs/lib/angular.min.js:6:192)
at Qb (http://localhost:8080/angularjs/lib/angular.min.js:92:109)
at c (http://localhost:8080/angularjs/lib/angular.min.js:93:295)
at h (http://localhost:8080/angularjs/lib/angular.min.js:77:437)
at http://localhost:8080/angularjs/lib/angular.min.js:78:169
这是我的代码:
索引.html:
<!doctype html>
<html ng-app>
<head>
<script src="lib/angular.min.js"></script>
<script src="js/indexApp.js"></script>
</head>
<body>
<div>
<div ng-controller="AjaxController">
{{users.data}}
</div>
</div>
</body>
</html>
indexApp.js
function AjaxController($scope, $http) {
$scope.beers = [ 0, 1, 2, 3, 4, 5, 6 ];
console.log("OMW");
$http({
method : 'GET',
url : 'data.json'
}).success(function(data, status, headers, config) {
$scope.users = data;
}).error(function(data, status, headers, config) {
$scope.users = "error" + data;
});
};
数据.json
{
success : "true",
data: [{name:"val"}]
}