Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 830
jonnie Avatar asked Aug 08 '13 15:08

jonnie


People also ask

How do I fix unexpected token in JSON error?

The "Unexpected token u in JSON at position 0" error occurs when we pass an undefined value to the JSON. parse or $. parseJSON methods. To solve the error, inspect the value you're trying to parse and make sure it's a valid JSON string before parsing it.

How do you fix uncaught SyntaxError unexpected end of JSON input?

You can solve the "Unexpected end of JSON input" error in the following 3 ways: wrap your parsing logic in a try/catch block. make sure to return a valid JSON response from your server. remove the parsing logic from your code if you are expecting an empty server response.

What is JSON parse?

JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON. parse() , as the Javascript standard specifies.


1 Answers

response is already parsed, you don't need to parse it again.

If you parse it again it will perform a toString-cast first so you're parsing

"[object Object]" 

which explains the unexpected token o.

like image 136
Halcyon Avatar answered Sep 26 '22 01:09

Halcyon