[英]How to return a List from controller to Ajax call and print it in the success?
This is the pattern of the object I'm getting when I printed out my list value from rest controller.这是我从 rest controller 打印出我的列表值时得到的 object 的模式。
[
{
"name": "Jon",
"isUser": true
},
{
"name": "Ween",
"isUser": false
}
]
but the problem is I dont know how can I get this value to my ajax call.但问题是我不知道如何将这个值传递给我的 ajax 调用。 I need that values for further work.
我需要这些价值观来做进一步的工作。 But whenever I'm calling this controller my
ajax
return error 500
.但是每当我调用这个 controller 我的
ajax
时返回error 500
。 this is my rest controller:这是我的 rest controller:
@RequestMapping(value = "/getusers", method = RequestMethod.GET)
public List userShow(HttpServletRequest request, Model model) {
List userlist = new ArrayList();
try{
userlist = JSONArray.fromObject(userService.getUserList());
System.out.println(userlist);
}catch (Exception e){
logger.error(e);
}
return userlist;
}
I can clearly see the list of the printed userList
from system.out
but i'm not sure why these values are not going in the ajax call
.我可以从
system.out
清楚地看到打印的userList
的列表,但我不确定为什么这些值不在ajax call
中。 Maybe I've to change the return
of my function, I've given list
as I want my data as the provided data I have given in the first part of the question.也许我必须更改我的 function 的
return
值,我已经给出了list
,因为我希望我的数据作为我在问题第一部分中给出的数据。 and my ajax call:和我的 ajax 通话:
$.ajax({
type: "GET",
url: "/getusers",
success: function (response) {
console.log(response);
if(response === true) {
user = true;
}
else{
user = false;
errorShow = "error getting values";
}
},
async: false
});
when this url hits, values are seen in the controller
but in the console.log
i see当这个 url 命中时,在
controller
中可以看到值,但在console.log
中我看到了
GET http://localhost:3000/getusers 500
获取http://localhost:3000/getusers 500
error.错误。 How can I get those values in the response section?
如何在响应部分获取这些值?
I think you are using @Controller annotation at the class level.我认为您在 class 级别使用 @Controller 注释。 If you use @RestController you will not have to write @Responsebody explicitly.
如果您使用@RestController,则不必显式编写@Responsebody。
Also instead of @RequestMapping(value = "/getusers", method = RequestMethod.GET) you can use new method annotations like so.此外,您可以使用新的方法注释来代替 @RequestMapping(value = "/getusers", method = RequestMethod.GET) 。 @GetMapping("/getusers")
@GetMapping("/getusers")
If you really want to return the List you can use ResponseEntity like this:如果你真的想返回列表,你可以像这样使用 ResponseEntity:
@RequestMapping(value = "/getusers", method = RequestMethod.GET)
public ResponseEntity <List> userShow(HttpServletRequest request, Model model) {
List userlist = new ArrayList();
try {
userlist = userService.getUserList();
System.out.println(a);
} catch (Exception e) {
logger.error(e);
}
URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/").buildAndExpand("").toUri();
return ResponseEntity.created(uri).body(userlist);
}
Notice that I got rid of the JSONArray.fromObject()
because ResposneEntity
will take of converting the List into JSON for you.请注意,我删除了
JSONArray.fromObject()
因为ResposneEntity
将为您将 List 转换为 JSON。 Also it's preferred to specify the List type, for example: List<User>
此外,最好指定 List 类型,例如:
List<User>
Just use @Responsebody
annotation on method level and you don't have to convert list to array anymore.只需在方法级别使用
@Responsebody
注释,您就不必再将列表转换为数组。 Return the list directly.直接返回列表。
Change改变
JSONArray.fromObject(userService.getUserList())
To至
userService.getUserList()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.