Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

following an example in an Angular book, and they have the following code:

<script>
   var test = {};


   var app = angular.module("app",[]);

   app.run(function($http){
      $http.get("/file").success(function(data){
         test.input = data;
      });
   });

   app.controller("ctrl",function($scope){
      $scope.handle = test;
   });


</script>

presumably data is pulled from file, pushed to the object 'test', which is then put into the scope's handle variable. The problem when I write this up, however, is that the $http.get:

   app.run(function($http){
      $http.get("/file").success(function(data){
         test = data;
      });
   });

is producing the following error:

SyntaxError: Unexpected string at Object.parse (native) at vc (http://localhost/common/js/frameworks/angular.min.js:15:480) at Zb (http://localhost/common/js/frameworks/angular.min.js:82:229) at http://localhost/common/js/frameworks/angular.min.js:83:143 at m (http://localhost/common/js/frameworks/angular.min.js:7:322) at dd (http://localhost/common/js/frameworks/angular.min.js:83:125) at d (http://localhost/common/js/frameworks/angular.min.js:84:380) at http://localhost/common/js/frameworks/angular.min.js:119:113 at n.a.$get.n.$eval (http://localhost/common/js/frameworks/angular.min.js:133:221) at n.a.$get.n.$digest (http://localhost/common/js/frameworks/angular.min.js:130:233)
(anonymous function) b.$get (anonymous function)
a.$get.n.$eval a.$get.n.$digest a.$get.n.$apply h K
z.onload

which, to me, indicates that $http is not expecting a string url. Could someone explain this to me? Is the book's sample code wrong, or perhaps out of date?

edit:

the /file contents are as follows:

[{"a":"1","b":"2"},{"aa":"3","bb":"4"}]

edit 2:

From other SO articles Problems parsing a JSON response using AngularJS it looks like $http.get is supossed to be auto parsing

share|improve this question
    
Can you tell us the contents of /file? Because I believe /file isn't what you expect it to be. – Joseph the Dreamer Nov 10 at 1:49

1 Answer 1

up vote 1 down vote accepted

you should encode the data. and the error will disappear:

'[{"a":"1","b":"2"},{"aa":"3","bb":"4"}]'
share|improve this answer
    
where does the encoding information go? – David Torrey Nov 10 at 1:56
    
what file is it .php or just a. json file? – Shim br Nov 10 at 2:00
    
just a json file – David Torrey Nov 10 at 2:00
    
so try to do it as I posted, and let me know if you still encounter the error. – Shim br Nov 10 at 2:01
    
so the error disappeared and i checked, the information is at least transferring, but I had 2 issues: First, I was under the impression it can accept json. The second, is the quotes are copying as well, which doesnt make it a valid JSON object when transferred. seems kinda hacky to look for quotes at the beginning and end and remove them just to parse it – David Torrey Nov 10 at 2:08

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.