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]
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?
this is my controller
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;
//
}
this is my Service Method:
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;
}
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); ?>
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
Update
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;
}
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