AngularJS JSON 解析(ajax)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15972559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS JSON parse(ajax)
提问by mcadirci
I have been tring to make a ajax request but there seems to be a problem. When my json attributes names are in " ( like {"name":value"} ), it works but when attribute names are not. I have following excepiton
我一直在尝试提出 ajax 请求,但似乎有问题。当我的 json 属性名称在 " ( 如 {"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
Here is my code:
这是我的代码:
index.html:
索引.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
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, status, headers, config) {
$scope.users = "error" + data;
});
};
};
data.json
数据.json
{
success : "true",
data: [{name:"val"}]
}
回答by Ezekiel Victor
You mustwrap attribute names in ". This is the only way to specify valid transport JSON, which is stricter than object notation in an executable JavaScript context. Any JSON parser will fail if you try to use the more lax notation.
您必须将属性名称包装在". 这是指定有效传输 JSON 的唯一方法,它比可执行 JavaScript 上下文中的对象表示法更严格。如果您尝试使用更宽松的表示法,任何 JSON 解析器都会失败。
See also the spec for JSONwhich mandates this.
另请参阅强制执行此操作的JSON 规范。
回答by nomad
Even I had the similar issue and the solution is, your string data should be in a specific format for JSON.parseor angular.fromJsonto work.
即使我有类似的问题,解决方案是,您的字符串数据应该采用特定格式,以便JSON.parse或angular.fromJson工作。
For ex:
例如:
var myString = '{"name":"nomad"}';
console.log(JSON.parse(myString));
The console output is: Object {name: "nomad"}
控制台输出为:Object {name: "nomad"}
回答by Deke
I had the same error. One of the firstName was an empty string like this.
{"firstName":"",
"lastName":"Doe"}edit: This error can also mean there are some syntax errors in your json file mainly. for example commenting out some things within the json file or not giving space between commas separating two key value pairs also threw the same error. Very beginner mistake thought this would help others.
我有同样的错误。其中一个 firstName 是一个像这样的空字符串。
{"firstName":"",
"lastName":"Doe"}编辑:此错误也可能意味着您的 json 文件中存在一些语法错误。例如,注释掉 json 文件中的某些内容或不在分隔两个键值对的逗号之间留出空格也会引发相同的错误。非常初学者的错误认为这会帮助其他人。
回答by falsarella
In my case, it was returning the following error:
就我而言,它返回以下错误:
SyntaxError: Unexpected token '
at Object.parse (native)
at fromJson (http://www.example.com/bower_components/angular/angular.js:1072:14)
语法错误:意外的标记 '
在 Object.parse(本机)
在 fromJson ( http://www.example.com/bower_components/angular/angular.js:1072:14)
So we found out that what was happening internally on angular was the following:
所以我们发现 angular 内部发生的事情如下:
JSON.parse("{'error': 'message'}"); // error
And indeed it returns a parser error, since it's expected the JSON to have strings wrapped with ", as the accepted answer points out.
事实上,它返回一个解析器错误,因为它期望 JSON 包含用 包裹的字符串",正如接受的答案指出的那样。
So, we just fixed the server response to comply with the JSON string specification:
因此,我们只是修复了服务器响应以符合JSON 字符串规范:
JSON.parse('{"error": "message"}'); // works!

