Uncaught SyntaxError: Unexpected token o

Uncaught SyntaxError: Unexpected token o

转载自:http://codego.net/600533/

我已经看过了所有的SO和谷歌,但也已经注意到,帮助,我不知道该如何继续。我有我从返回一个数组echo json_encode()看起来像这样:

[" "," "," "," "," ",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]

但我不断收到Unexpected token o at Object.parse (native)当我尝试JSON.parse()和Unexpected token o at Function.parse (native)当JQuery的alternitive。 但是当我只需将它连接到$scope我可以在页面上打印出来,所以我在做什么错了,我该如何修复? 这是我的控制器

function myController($scope, memberFactory) { response = memberFactory.getMonth("2013-08-01 06:30:00"); //$scope.response = response; var monthDays = $.parseJSON(response); 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; // }

这是我服务的方法:

obj.getMonth = function (date) { var month = $q.defer(); $http.get('getMonth.php?date=' + date) .success(function (data, status, headers, config) { month.resolve(data); }); return month.promise; }

这就是:

<?php $daysOfMonth=[ " ", " ", " ", " ", " ",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]; echo json_encode($daysOfMonth); ?>

事情我已经试过了 如果我返回typeof我得到的对象,所以后来我都试过var monthDays = Array.prototype.slice.call(response)var monthDays = $.map(response, function (value, key) { return value; });在这里的答案建议 CodeGo.net,我自己也尝试JSON.stringify()但我只是得到{}作为结果 我真的有这样的一个真正需要的正确方向 更新 我相信这可能是一个问题$q.defer()我更新了我的getMonth方法如下:

obj.getMonth = function (date) { var month = $q.defer(); $http.get('getMonth.php?date=' + date) .success(function (data, status, headers, config) { month.resolve(data); console.log("data " + data[0]); console.log("resolved " + month.promise[0]); }); return month.promise; }

现在我得到1console.log("data " + data[0]);如预期,但我得到的console.log(month);我得到[object Object]console.log(month.promise)我得到[object Object]console.log(month.promise[0]);我得到undefined
本文地址 :CodeGo.net/600533/ 
------------------------------------------------------------------------------------------------------------------------- 
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



本文标题 :是什么原因造成“未捕获的SyntaxError:意外的标记O”与$ parseJSON()和JSON.parse()。


转载于:https://my.oschina.net/yuewawa/blog/609933

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值