0

I've tried searching through StackOverflow regarding this issue but could not find a solution that worked for me. I'm using AngularJS. I have simple controller that calls a http service and reads the response data. In this specific case, I have a JSON output with an objects array I am unable to read.

Here's the JSON output:

{
  "results": [{
    "id": "1",
    "download_url": "",
    "uploader": {
      "id": "114899"
    },
    "uploaded": "1442599380",
    "streaming_url_timeout": 1446092554
  }, {
    "id": "2",
    "download_url": "",
    "uploader": {
      "id": "114899"
    },
    "uploaded": "1442599380",
    "streaming_url_timeout": 1446092554
  }]
}

I'm trying to get access to items in 'results'. This is my Service that retrieves the JSON data:

this.getUData = function() {
  var deferred = $q.defer();

  $http.jsonp(API_URL + 'udata')
    .success(function(data) {
      deferred.resolve(data);
    })
    .error(function(data) {
      deferred.reject(data);
    });

  return deferred.promise;
}

And then this is how i call this service from my controller:

myservices.getUData().then(function(data) {
   $scope.uitems = data.results;
});

Template:

<div class="item" href="#" ng-repeat="item in uitems">
  <h2>{{item.id}}</h2>
</div>

But when I try to access the items in 'results', I get the following error:

Uncaught SyntaxError: Unexpected token : 

The line in question for this error is "results":[

3
  • Try using ng-repeat="item in uitems.results". Becase the element results is an array of your json response on which you can use ng-repeat Commented Oct 29, 2015 at 4:33
  • @J-D, no change in result I'm afraid. Commented Oct 29, 2015 at 15:10
  • @ngLover, not solved. Commented Oct 29, 2015 at 15:10

1 Answer 1

0

Looks like in this case the callback function was not working correctly on the PHP server so I managed to get away by using the 'GET' method in the $http service like so:

this.getUData = function() {
  var deferred = $q.defer();

$http({method: 'GET', url: API_URL + 'udata'})
    .success(function(data) {
      deferred.resolve(data);
    })
    .error(function(data) {
      deferred.reject(data);
    });

  return deferred.promise;
}

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.