1

I have this grails controller which returns me a list of Strings:

class UserController {
  def getUsers = {
    def users = Lists.asList("user1", "user2", "user3")
    render users : users;
  }
}

And a javascript which calls the above controller:

    var displayUsers = function(users) {
       for (var i = 0; i < users.length; i++) {
          console.log(users[i];
// this iterates over each character in the users object and not over                 
// it's elements
// users objects is interpreted as a string object and not as a list
       }
    }

$(document).ready(function(){
    $('.expand-group').on('click', function(e) {
        $.ajax({
            url: '/getUsers',
            type: "GET",
            data: {}
        }).done(function(data, textStatus) {
               displayUsers(data);
        });
    });
});

My output is the following:

[
'
u
s
e
r
s
'
:
[
'
u
...

So how can I tell javascript to see my users object as an array?

1 Answer 1

1

you should send the response as a JSON object (from grails.converters package)

def getUsers = {
    def users = Lists.asList("user1", "user2", "user3")
    render users as JSON;
}
Sign up to request clarification or add additional context in comments.

1 Comment

glad to hear that (nod)

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.