$.parseJSON() 和 JSON.parse() 导致“Uncaught SyntaxError: Unexpected token o"的原因 [英] What is causing “Uncaught SyntaxError: Unexpected token o” with $.parseJSON() and JSON.parse()

查看:37
本文介绍了$.parseJSON() 和 JSON.parse() 导致“Uncaught SyntaxError: Unexpected token o"的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了 SO 和 Google,但注意到这有所帮助,我不确定如何继续.我有一个数组,我使用 echo json_encode() 从 php 页面返回,如下所示:

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]

但是当我尝试使用 JSON.parse() 和 Unexpected token o at Function.parse (native) 时,我不断收到 Unexpected token o at Object.parse (native)代码>当我使用 JQuery 替代方案时.

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.

但是当我将它附加到 $scope 时,我可以在页面上使用它打印出来,那么我做错了什么,我该如何纠正?

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:

<?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; });正如一些答案中的建议 here,我也试过 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;
}   

现在我得到了 console.log("data " + data[0]);1 和预期的一样,但我得到了 console.log(month); 我得到 [object Object] for console.log(month.promise) 我得到 [object Object] 和 forconsole.log(month.promise[0]); 我得到 undefined

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 已经解析完毕,不需要再次解析.

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

解释了unexpected token o.

这篇关于$.parseJSON() 和 JSON.parse() 导致“Uncaught SyntaxError: Unexpected token o"的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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