Answer the question
In order to leave comments, you need to log in
Am I reading JSON correctly?
I'm trying to read JSON into a variable, but I'm getting an error. Here is the code itself:
$scope.questions = $http.get('js/questions-content.json').success(function(response) {
return response.data;
// console.log(response.data);
});
SyntaxError: Unexpected token ,
at Object.parse (native)
at fromJson
[{"id":1},{"id":2}]
Answer the question
In order to leave comments, you need to log in
If you call success, then the callback argument is no longer response, but response.data.
If you call then, then there is a response.
I could have tried deriving the success callback argument myself before asking.
Well, from a promise you get not a value, but only a promise. So the $scope.questions variable will contain the promise of the result, not the result itself.
$http.get('js/questions-content.json').success(function(questions) {
$scope.questions = questions;
});
// Но лучше так:
$http.get('js/questions-content.json').then(function(response) {
$scope.questions = response.data;
});
Because it's done asynchronously:
$http.get('js/questions-content.json').success(function(response) {
return response.data;
// console.log(response.data);
});
$http.get('js/questions-content.json').success(function(response) {
$scope.questions = response.data;
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question