AngularJS JSON parse (ajax)

I am trying to make an ajax request, but it seems to be a problem. When my json attribute names are in "(for example, {" name ": value"}), it works, but when the attribute names are missing. I have the following excepiton

SyntaxError: Unexpected token s at Object.parse (native) at pb (http://localhost:8080/angularjs/lib/angular.min.js:12:472) at Vc.d.defaults.transformResponse (http://localhost:8080/angularjs/lib/angular.min.js:92:314) at http://localhost:8080/angularjs/lib/angular.min.js:92:127 at Array.forEach (native) at n (http://localhost:8080/angularjs/lib/angular.min.js:6:192) at Qb (http://localhost:8080/angularjs/lib/angular.min.js:92:109) at c (http://localhost:8080/angularjs/lib/angular.min.js:93:295) at h (http://localhost:8080/angularjs/lib/angular.min.js:77:437) at http://localhost:8080/angularjs/lib/angular.min.js:78:169 

Here is my code:

index.html

 <!doctype html> <html ng-app> <head> <script src="lib/angular.min.js"></script> <script src="js/indexApp.js"></script> </head> <body> <div> <div ng-controller="AjaxController"> {{users.data}} </div> </div> </body> </html> 

indexApp.js

 function AjaxController($scope, $http) { $scope.beers = [ 0, 1, 2, 3, 4, 5, 6 ]; console.log("OMW"); $http({ method : 'GET', url : 'data.json' }).success(function(data, status, headers, config) { $scope.users = data; }).error(function(data, status, headers, config) { $scope.users = "error" + data; }); 

};

data.json

 { success : "true", data: [{name:"val"}] } 
+7
source share
4 answers

You must wrap the attribute names in. " This is the only way to specify a valid transport JSON that is stricter than the designation of the object in an executable JavaScript context. Any JSON parser will not work if you try to use a weaker notation.

See also the specification for JSON that provides this.

+18
source

Even I had a similar problem and solution, your string data should be in a specific format for JSON.parse or angular.fromJson .

For ex:

 var myString = '{"name":"nomad"}'; console.log(JSON.parse(myString)); 

Console Output: Object {name: "nomad"}

+8
source

I had the same error. One of firstName was an empty string like this. {"firstName":"", "lastName":"Doe"} edit: This error may also mean that there are some syntax errors in your json file. for example, commenting on some things in a json file or not giving a space between commas separating two pairs of key values ​​also throws the same error. A very initial mistake was that it would help others.

+1
source

In my case, it returned the following error:

Syntax Error: Unexpected Token

in Object.parse (native)

at fromJson ( http://www.example.com/bower_components/angular/angular.js:1072:14 )

So, we learned that the following happens inside angular:

 JSON.parse("{'error': 'message'}"); // error 

And indeed, it returns a parser error, since it was expecting JSON to have lines wrapped with " as the accepted answer indicates .

So, we just fixed the server response to match the JSON specification:

 JSON.parse('{"error": "message"}'); // works! 
0
source

All Articles