是什么导致了$.parseJSON()和JSON.parse()的“Uncaught SyntaxError:意外令牌o

What is causing “Uncaught SyntaxError: Unexpected token o” with $.parseJSON() and JSON.parse()

本文关键字:Uncaught SyntaxError 令牌 意外 JSON parseJSON 是什么 parse      更新时间:2023-09-26

我已经看遍了SO和谷歌,但注意到这有帮助,我不确定如何继续。我有一个数组,我使用echo json_encode()从php页面返回,它看起来像这样:

[" "," "," "," "," ",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 Object.parse (native),而当我使用JQuery变量时,我得到Unexpected token o at Function.parse (native)

但当我把它附加到$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:

<?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,我会得到Object,所以我已经尝试了var monthDays = Array.prototype.slice.call(response)var monthDays = $.map(response, function (value, key) { return value; });,正如这里的一些答案所建议的那样,我也尝试过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;
}   

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

response已经解析,不需要再次解析。

如果您再次解析它,它将首先执行toString强制转换,因此您正在解析

"[object Object]"

这解释了CCD_ 20。

请查看$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 

这是在@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;
});

如果发送了标头:

Content-Type: text/json

则不需要调用CCD_ 21。例如:

在PHP中,我会这样设置标题:

<?php
header("Content-Type: text/json");
echo json_encode(array("someKey" => "someValue"));

在Javascript中,我会有这样的成功函数:

success: function(response) {
// This will output "someValue" to the console.
// Note the lack of the parseJSON() call. parseJSON() is called for us because the header was sent as text/json
console.log(response.someKey);
}