27

我已经查看了 SO 和 Google,但注意到这有所帮助,我不确定如何继续。我有一个从 php 页面返回的数组,使用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 替代方案时,我不断得到。

但是当我把它附加到$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我得到对象,那么我已经尝试过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;
}   

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

4

4 回答 4

65

response已经解析过了,不需要再解析了。

如果您再次解析它,它将首先执行 toString-cast,因此您正在解析

"[object Object]"

这解释了unexpected token o.

于 2013-08-08T15:32:55.633 回答
6

请查看$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 
于 2013-08-08T15:37:52.030 回答
1

这在@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;
});
于 2013-08-09T11:34:02.460 回答
1

如果发送了标头:

Content-Type: text/json

然后parseJSON()不需要调用。例如:

在 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);
}
于 2014-02-23T19:10:52.510 回答