6
searching for data from an api through $http.get, but I find an error.
My request within a service:
app.service('pessoas',function($http){
this.getHumanos = function(callback){
$http.get('http://private-ff1c4-grupo.apiary-mock.com/pessoas').success(callback);
};
});
and here where inside my controller where I get the data:
app.controller('servico',function($scope, pessoas){
pessoas.getHumanos(function(data){
$scope.pessoas = data;
});
});
Error shown in console:
SyntaxError: Unexpected token i
at Object.parse (native)
at pc (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:14:208)
at Zb (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:76:379)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:77:237
at s (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:7:302)
at Zc (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:77:219)
at c (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:78:349)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:112:20
at l.$eval (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:125:305)
at l.$digest (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:122:398)
Try to put the
error
calling also the callback. Observing: those functionssuccess
anderror
is depreciated, manipulates with Promise.– akira-ito
Hello, I put the error too, I did not understand the part of Promise, but well, I keep getting the same error.
– Henrique Silva
Face this is not syntax error? What is this
i
? Doesn’t have ai
lost in the code?– akira-ito
There’s no i in the code I’ve reviewed several times haha, look here: https://github.com/HenriRique/angularHttp
– Henrique Silva
In the answer to the url
http://private-ff1c4-grupo.apiary-mock.com/pessoas
the key age is not in quotes. Try to leave it in quotes.– akira-ito
It worked Buddy, just one more little doubt it’s returning me array, how do I loop in ng-repeat? pq the list is appearing empty
– Henrique Silva
ng-repeat="item in pessoas"
the nameitem
can be anyone you want andpessoas
is the name of$scope
which contains the data. If you are using a<ul>
ng-repeat must be placed in the<li>
, that is, always in the element you want to repeat.<li ng-repeat="item in pessoas"> {{item.nome}} </li>
– celsomtrindade