我已经查看了 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;
}
现在我得到1了console.log("data " + data[0]);预期但我得到了console.log(month);我得到[object Object]了console.log(month.promise)我得到[object Object]了console.log(month.promise[0]);我得到了undefined