我一直在努力提出ajax请求,但似乎有问题.当我的json属性名称在“(like {”name“:value”})中时,它可以工作,但属性名称不是.我有以下例外情况
@H_301_22@
您必须在“.”中包装属性名称.这是指定有效传输JSON的唯一方法,它比可执行JavaScript上下文中的对象表示法更严格.如果您尝试使用更宽松的表示法,任何JSON解析器都将失败.
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
这是我的代码:
index.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,config) { $scope.users = "error" + data; });
};
data.json
{ success : "true",data: [{name:"val"}] }
另见the spec for JSON要求这样做.