是什么原因造成“未捕获的SyntaxError:意外的令牌O”与$ .parseJSON()和JSON.parse() [英] What is causing “Uncaught SyntaxError: Unexpected token o” with $.parseJSON() and JSON.parse()

查看:146
本文介绍了是什么原因造成“未捕获的SyntaxError:意外的令牌O”与$ .parseJSON()和JSON.parse()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处都找过SO和谷歌但也指出,已帮助,我不知道该如何继续。我有我从一个PHP页面中使用返回一个数组回声json_en code(),看起来像这样:

I Have looked all over SO and Google but have have noting that has helped and I'm unsure how to continue. I have an array which I am returning from a php page using echo json_encode() that looks like this:

[" "," "," "," "," ",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]

但我不断收到意外标记Ø在Object.parse(本机)当我尝试使用JSON.parse()来和意外标记Ø在Function.parse(本机)当我使用JQuery的alternitive。

but I keep getting Unexpected token o at Object.parse (native) when I try to use JSON.parse() and Unexpected token o at Function.parse (native) when I use the JQuery alternitive.

但是当我只是其附加到 $范围我可以使用页面上打印出来,所以我在做什么错了,我该怎么纠正呢?

but when I just attach it to the $scope I can print it out using on the page, so What am I doing wrong and how can I correct this?

这是我的控制器

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:

and this is my php:

<?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(响应) VAR monthDays = $ .MAP(响应函数(值,键){返回值;}); 在一些答案建议< A HREF =htt​​p://stackoverflow.com/questions/3865139/cast-javascript-object-to-array-how-to>这里,我也曾尝试 JSON.stringify (),但我只是得到 {} 的结果

Things I have Tried

If I return the typeof I get Object, so I have then tried var monthDays = Array.prototype.slice.call(response) and var monthDays = $.map(response, function (value, key) { return value; });as suggested in some of the answers here, I have also tried JSON.stringify() but I just get {} as the result

我真的很沮丧,这真的是需要有人点我在正确的方向

I am really frustrated with this an really need someone to point me in the correct direction

我认为这可能是使用问题 $ q.defer(),因为我更新了我的getMonth方法如下:

I believe this may be an issue with using $q.defer() as I updated my getMonth Method as follows:

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;
}   

现在我得到 1 的console.log(数据+数据[0]); 预期但我得到了的console.log(月); 我得到 [对象的对象] 的console.log(month.promise)我得到 [对象的对象] 的console.log(month.promise [ 0]); 我得到未定义

now I get 1 for console.log("data " + data[0]); as expected but I get for console.log(month); I get [object Object] forconsole.log(month.promise) I get [object Object]and for console.log(month.promise[0]); I get undefined

推荐答案

响应已经被解析,你不需要再解析它。

response is already parsed, you don't need to parse it again.

如果您再次解析它,所以你要分析它会首先执行一个toString铸

If you parse it again it will perform a toString-cast first so you're parsing

"[object Object]"

这也解释了意外的标记0

这篇关于是什么原因造成“未捕获的SyntaxError:意外的令牌O”与$ .parseJSON()和JSON.parse()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆