加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS JSON解析(ajax)

发布时间:2020-12-17 07:24:32 所属栏目:安全 来源:网络整理
导读:我一直在努力提出ajax请求,但似乎有问题.当我的json属性名称在“(like {”name“:value”})中时,它可以工作,但属性名称不是.我有以下例外情况 SyntaxError: Unexpected token sat Object.parse (native)at pb (http://localhost:8080/angularjs/lib/angular
我一直在努力提出ajax请求,但似乎有问题.当我的json属性名称在“(like {”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

这是我的代码:

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"}]
}
您必须在“.”中包装属性名称.这是指定有效传输JSON的唯一方法,它比可执行JavaScript上下文中的对象表示法更严格.如果您尝试使用更宽松的表示法,任何JSON解析器都将失败.

另见the spec for JSON要求这样做.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读