gpt4 book ai didi

javascript - 如何在 JavaScript 中迭代 Grails 列表

转载 作者:行者123 更新时间:2023-11-27 22:58:20 26 4
gpt4 key购买 nike

我有这个 grails Controller ,它返回一个字符串列表:

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

还有一个调用上述 Controller 的 JavaScript:

    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);
});
});
});

我的输出如下:

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

那么我如何告诉 javascript 将我的用户对象视为数组?

最佳答案

您应该将响应作为 JSON 对象发送(来自 grails.converters 包)

def getUsers = {
def users = Lists.asList("user1", "user2", "user3")
render users as JSON;
}

关于javascript - 如何在 JavaScript 中迭代 Grails 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37378596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com