意外令牌{解析时有效' JSON

时间:2014-05-08 18:03:46

标签: json angularjs

当我按下按钮时,我试图将存储在json中的数组分配到控制器中的数组中。我已经对几个在线验证器运行了我的json,并且所有人都声称我的json是有效的。但是,当我去解析它时,我得到一个意外的令牌错误。

我已经仔细检查了所有内容,我认为所有内容都与我在https://docs.angularjs.org/tutorial/step_05处理的教程非常相似,但它只是不想工作。

相关控制线:

angular.module('myApp.controllers', [])
  .controller('PlayerAdditionCtrl', function($scope, $http) {
        $scope.players = [];
        $scope.loadDefaults = function()
        {
            $http.get('defaults.json').success(function(array)
            {
                        //assignment here
            });
        }
  });

完成json:

[
    { 
        "name": "Mike",
        "color": "teal"
    },
    {
        "name": "Madre",
        "color": "seagreen"
    },
    {
        "name": "Anthony",
        "color": "royalblue"
    },
    {
        "name": "GI-Joe",
        "color": "olivedrab"
    }
]

Chrome中的完整错误文字:

    SyntaxError: Unexpected token {
        at Object.parse (native)
        at fromJson (http://localhost:8000/app/bower_components/angular/angular.js:1078:14)
        at $HttpProvider.defaults.defaults.transformResponse (http://localhost:8000/app/bower_components/angular/angular.js:7317:18)
        at http://localhost:8000/app/bower_components/angular/angular.js:7292:12
        at Array.forEach (native)
        at forEach (http://localhost:8000/app/bower_components/angular/angular.js:323:11)
        at transformData (http://localhost:8000/app/bower_components/angular/angular.js:7291:3)
        at transformResponse (http://localhost:8000/app/bower_components/angular/angular.js:7963:17)
        at wrappedCallback (http://localhost:8000/app/bower_components/angular/angular.js:11319:81)
        at http://localhost:8000/app/bower_components/angular/angular.js:11405:26 angular.js:9778
    (anonymous function) angular.js:9778
    (anonymous function) angular.js:7216
    wrappedCallback angular.js:11322
    (anonymous function) angular.js:11405
    Scope.$eval angular.js:12412
    Scope.$digest angular.js:12224
    Scope.$apply angular.js:12516
    done angular.js:8204
    completeRequest angular.js:8412
    xhr.onreadystatechange angular.js:8351

1 个答案:

答案 0 :(得分:3)

再次检查你的json /和/或/ pathtojson,你的代码适用于我

http://plnkr.co/edit/AGdJ9SasmHn74RQe8xX2?p=preview