Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Question from AngularJS noob.

I am trying to use an asmx web service to display grid. I tested the web service and it correctly outputs the JSON data. Here is my controller

app.controller('SetupController', ['$scope', '$http', function ($scope, $http) {

    var url = 'app/pricefilessetup/grid.asmx/getGridJson';

    $http.get(url).success(function (data) {
        var myjson = JSON.parse(data);
        $scope.products= JSON.parse(myjson);
    });
}]);

For some reason, SO is not allowing me to paste the html but it basically has a ng-controller directive and ng-repeat to loop through the JSON data.

When I run this web app, I get the error

SyntaxError: Unexpected token o at Object.parse (native) and it points to following line

  $scope.questions = JSON.parse(myjson);

I tried checking the value of myjson using alert and it displays [object Object], [object Object], ...

Is there anything I am missing here

share|improve this question
1  
I doubt you need to ever use JSON.parse. Angular expects the response to be JSON by default and deserialises it for you. –  Phil Apr 21 at 5:23
1  
Also, alert is a terrible debugging tool. Use console.log instead (or Angular's $log service) –  Phil Apr 21 at 5:26

3 Answers 3

up vote 1 down vote accepted

I think data returned is already in JSON, no need of JSON.parse(), unless it in string format.

$scope.products= data;
share|improve this answer

Your variable myjson is already a valid JavaScript Object. You do not have to use JSON.parse on it.

share|improve this answer

Why you using JSON.parse in two times?

 var myjson = JSON.parse(data);
  $scope.products= JSON.parse(myjson);

You have already parse the data object,So then why you parsing another one time?

also i think your data is return json result, so you don't need to pars the object

just use this

$scope.products= data;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.