是什么原因造成“未捕获的SyntaxError:意外的标记O”与$ parseJSON()和JSON.parse()。
1. response
已经解析,你不需要再解析它。 如果再次解析它会首先执行一个toString铸所以你像解析"[object Object"]
这也解释了unexpected token o
2. 这是通过替换为@CuongLe这里帮助解决
response = memberFactory.getMonth("2013-08-01 06:30:00");
var monthDays = $.parseJSON(response);
有:
response = memberFactory.getMonth("2013-08-01 06:30:00");
response.then(
function (monthDays) {
console.log("monthDays : " + monthDays + " !!!");
var dates = [];
for (var i = 0; i < monthDays.length; i++) {
if (i % 7 == 0) dates.push([]);
dates[dates.length - 1].push(monthDays[i]);
}
$scope.dates = dates;
});
3. 请看看一个章节的转变$ HTTP模块的请求和响应。 如果JSON响应被检测到,反序列化JSON解析器。 因为它已经被解析为JSON对象,如果你再分析它,你会得到这个错误。 下面是一个简单的测试:
response = '{"a": "a","b": "b"}';
var obj = $.parseJSON(response);
console.log(obj); //Object {a: "a", b: "b"}
$.parseJSON(obj) //Uncaught SyntaxError: Unexpected token o