Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is causing “Uncaught SyntaxError: Unexpected token o” with $.parseJSON() and JSON.parse()
    primarykey
    data
    text
    <p>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 <code>echo json_encode()</code> that looks like this:</p> <pre><code>[" "," "," "," "," ",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] </code></pre> <p>but I keep getting <code>Unexpected token o at Object.parse (native)</code> when I try to use JSON.parse() and <code>Unexpected token o at Function.parse (native)</code> when I use the JQuery alternitive.</p> <p>but when I just attach it to the <code>$scope</code> I can print it out using on the page, so What am I doing wrong and how can I correct this?</p> <p>this is my controller</p> <pre><code>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 &lt; monthDays.length; i++) { if (i % 7 == 0) dates.push([]); dates[dates.length - 1].push(monthDays[i]); } $scope.dates = dates; // } </code></pre> <p>this is my Service Method:</p> <pre><code>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; } </code></pre> <p>and this is my php:</p> <pre><code>&lt;?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); ?&gt; </code></pre> <h2>Things I have Tried</h2> <p>If I return the <code>typeof</code> I get Object, so I have then tried <code>var monthDays = Array.prototype.slice.call(response)</code> and <code>var monthDays = $.map(response, function (value, key) { return value; });</code>as suggested in some of the answers <a href="https://stackoverflow.com/questions/3865139/cast-javascript-object-to-array-how-to">here</a>, I have also tried <code>JSON.stringify()</code> but I just get <code>{}</code> as the result</p> <p>I am really frustrated with this an really need someone to point me in the correct direction</p> <h2>Update</h2> <p>I believe this may be an issue with using <code>$q.defer()</code> as I updated my getMonth Method as follows:</p> <pre><code>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; } </code></pre> <p>now I get <code>1</code> for <code>console.log("data " + data[0]);</code> as expected but I get for <code>console.log(month);</code> I get <code>[object Object]</code> for<code>console.log(month.promise)</code> I get <code>[object Object]</code>and for <code>console.log(month.promise[0]);</code> I get <code>undefined</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload