Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

This question already has an answer here:

My REST server is returning the following from /blog:

["hello","yo"]

This is my entire angularjs app:

var myapp = new angular.module("myapp", ["ngResource"]);

myapp.controller("MainCtl", ["$scope", "$resource", function($scope, $resource){
    var Blog = $resource("/blog/:entry", {entry: '@entry'});
    $scope.entries = Blog.query();
}]);

When I put {{entries}} somewhere in the html, I see this: [{"0":"h","1":"e","2":"l","3":"l","4":"o"},{"0":"y","1":"o"}]

Somehow angular is misinterpreting this array of values. I also tried this with exactly the same results:

myapp.controller("MainCtl", ["$scope", "$resource", function($scope, $resource){
    var Blog = $resource("/blog/:entry", {entry: '@entry'}, {
        list: {
            url: "/blog",
            method: "GET",
            isArray: true,
            transformResponse: function(data, headers) {
                var h = ["hello","yo"];
                console.log(h);
                return h;
            }
        }
    });
    $scope.entries = Blog.list();
}]);

Interestingly, that console.log call prints the correct value.

share|improve this question

marked as duplicate by Ilan Frumer, Jake, Stewie, Greg, hardmath Mar 8 '14 at 2:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
This is angular 1.2.10 by the way. – Jake Jan 25 '14 at 21:52
    
This is most likely how you're sending the response from your server side. What are you using there? – Steve Davis Jan 25 '14 at 22:00
    
pastebin.com/kZJkRp0C for example – Jake Jan 25 '14 at 22:08

1 Answer 1

This is definitely a duplicate of One dimensional array of strings being parsed to 2d by angular resource

When using $resource, if isArray = false, the expected response is a JSON object:

{
   "foo" : "bar"
}

If isArray = true, the expected response is supposed to be a JSON array of objects:

[
    { "foo" : "bar" },
    { "faa" : "bah" }
]

You have three options in this situation:

  1. Use $http instead of $resource
  2. Use $http's transformResponse functionality (see http://docs.angularjs.org/api/ng.$http)
  3. If you have control over the API, you can change the structure the response you are sending. Depending on semantics, you could send an array of objects (use isArray=true), or an object containing an array (use isArray=false). Whichever makes more sense to you.
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.