【问题标题】:Angular not parsing jsonAngular不解析json
【发布时间】:2014-04-05 12:03:33
【问题描述】:

我有一个试图通过 $http 服务访问的静态 json 文件。静态资源位于 data/user/person.json。 $http 服务发出请求并返回预期结果,内容类型为:应用程序/json 和状态码:200。

然而,在 Angular 方面,代码进入了“catch”回调,而不是成功并出现以下错误:

 SyntaxError: Unexpected token '. 

' 指的是大括号后响应的第一个字符。如果我将 'name' 更改为 name,我会得到 SyntaxError:

SyntaxError: Unexpected token n. 

为什么 Angular 难以解析这个 json 响应?

json响应及相关代码如下:

{'name':'johndoe', 'age':99}

function getUser() {
    $http({url: 'data/user/person.json', method: 'GET'}).success(function(data) {
        console.log(data);
    }).error(function(data, status, header, config) {
        console.log('status:' + status);
    }).catch(function(err) {
        //this block is entered
        console.log(err);
    });
};

这是错误信息的完整打印输出:

SyntaxError: Unexpected token n
at Object.parse (native)
at Yb (http://localhost:8000/Scripts/angular/angular.min.js:14:208)
at $d.e.defaults.transformResponse (http://localhost:8000/Scripts/angular/angular.min.js:67:366)
at http://localhost:8000/Scripts/angular/angular.min.js:67:127
at Array.forEach (native)
at r (http://localhost:8000/Scripts/angular/angular.min.js:7:280)
at wc (http://localhost:8000/Scripts/angular/angular.min.js:67:109)
at c (http://localhost:8000/Scripts/angular/angular.min.js:68:297)
at I (http://localhost:8000/Scripts/angular/angular.min.js:97:187)
at http://localhost:8000/Scripts/angular/angular.min.js:98:350 

【问题讨论】:

    标签: javascript json angularjs


    【解决方案1】:

    要让 json 成为 valid json,您的数据结构应该是这样的

    {"name":"johndoe", "age":99}

    双引号,以及您的按键。

    【讨论】:

      猜你喜欢
      • 2019-01-17
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多