2014-04-05 29 views
1

我有一個靜態json文件,我試圖通過$ http服務訪問。靜態資源位於data/user/person.json。 $ http服務發出一個請求並返回預期的結果,內容類型爲application/json和statuscode:200。角不解析json

在角側然而,代碼進入「捕獲」回調,而不是與下面的錯誤的成功:

SyntaxError: Unexpected token '. 

的「指的是第一個字符在花括號後的響應。如果我改變「名稱」,以僅舉我會得到語法錯誤:

SyntaxError: Unexpected token n. 

爲什麼角有diffuculty解析此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 

回答

4

對JSON是valid json你的數據結構應該是這樣的

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

雙引號,也圍繞你的鑰匙。