1

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

1
  • Can you tell us the contents of /file? Because I believe /file isn't what you expect it to be. Commented Nov 10, 2015 at 1:49

1 Answer 1

2

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

'[{"a":"1","b":"2"},{"aa":"3","bb":"4"}]'
Sign up to request clarification or add additional context in comments.

6 Comments

what file is it .php or just a. json file?
so try to do it as I posted, and let me know if you still encounter the error.
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
another odd issue is that with an auto-refresh it will show ass i type in {{handle}}, then when i refresh the content disappears
you can use JSON too, something like: {"recs": [{"a":"1","b":"2"},{"aa":"3","bb":"4"}]}, and then in your function you fetch data.recs.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.