1

I have an API built in padrino (ruby framework) that is returning api data in json format, and I'm building an angular app on top of it to read/create/update/delete from it.

When I do a console.log(data) below I get the horrible resource character by character output instead of some kind of nice pretty json format I want. How do I convert it to json so that I can then use the data in the Angular view that will show a list of heroes?

json format I want

Something like:

{[superhero_name: "SpiderMan", id: "1", age: "25"]}

actual output

Resource {0: "[", 1: "{", 2: """, 3: "i", 4: "d", 5: """, 6: ":", 7: "1", 8: ",", 9: """, 10: "s", 11: "u", 12: "p", 13: "e", 14: "r", 15: "h", 16: "e", 17: "r", 18: "o", 19: "_", 20: "n", 21: "a", 22: "m", 23: "e", 24: """, 25: ":", 26: """, 27: "B", 28: "a", 29: "t", 30: "m", 31: "a", 32: "n", 33: """, 34: ",", 35: """, 36: "a", 37: "g", 38: "e", 39: """, 40: ":", 41: "3", 42: "0", 43: "}", 44: "]", $promise: Promise, $resolved: true}

angular code

'use strict';

(function(angular) {
  function ApiAction($resource) {
    return $resource('/api/',
      { },
      { api_index: {
        method: "GET",
        isArray: false 
        }
      }
                    );
  }

  function heroCtr($scope, ApiAction) {

    $scope.heroes = ApiAction.api_index({}, {});
    $scope.heroes.$promise.then(function(data) {
      console.log(data);
    }, function(data) {
    });
  }

  var heroApp = angular.module('heroApp', ['ngResource']);
  heroApp.controller('heroCtr', ['$scope', 'ApiAction', heroCtr]);
  heroApp.factory('ApiAction', ['$resource', ApiAction]);
})(angular);
Nona
  • 5,302
  • 7
  • 41
  • 79
  • 1
    Correct response should be `{superhero_name: "SpiderMan", id: "1", age: "25"}`. – dfsq Jul 28 '15 at 05:29
  • 1
    Query your API using curl or POSTMAN extension - if the output is still the same as you have mentioned, then you need to correct how your API returns the data. You don't make the changes in AngularJS but on the server side in this case – callmekatootie Jul 28 '15 at 05:53
  • Thanks, I was calling to_json on the backend which was stringify-ing the json. – Nona Jul 29 '15 at 05:48

0 Answers0