Reputation:
activeUsers.getUsers()
returns a list with the online users in my app.
I want to display it in my HTML but I have no idea how to do this. I'm getting error 404 on my ajax request yet if I put a breakpoint in my controller it shows the list of online users.
Any help??
Controller
@RequestMapping(value = "/loggedUsers", method = RequestMethod.GET)
public String getLoggedUsers(Model model) {
model.addAttribute("loggedUsers", activeUsers.getUsers());
return "loggedUsers";
}
AJAX
$(document).ready(function () {
//get loggedUsers
$.ajax({
url: '/loggedUsers',
type: 'get',
success: function (loggedUsers) {
console.log(loggedUsers);
}
});
});
HTML
<div id="sidebar-right" class="visible">
<ul class="sidebar-nav">
<li id="dashboard">
<a href="dashboard">
<c:forEach var="loggedUser" items="${loggedUsers}">
<span class="menu-title">${loggedUser}</span>
</c:forEach>
</a>
</li>
</ul>
the console log returns:
error 404
Upvotes: 0
Views: 83
Reputation: 187
Add @Response
body. You need to make this a REST API because it's a AJAX call.
@RequestMapping(value = "/loggedUsers", method = RequestMethod.GET)
public @ResponseBody String getLoggedUsers(Model model) {
model.addAttribute("loggedUsers", activeUsers.getUsers());
return "loggedUsers";
}
Upvotes: 1
Reputation: 10148
Change your controller and add @ResponseBody
annotation above it. Your code should look like
@RequestMapping(value = "/loggedUsers", method = RequestMethod.GET)
@ResponseBody
public String getLoggedUsers(Model model) {
model.addAttribute("loggedUsers", activeUsers.getUsers());
return "loggedUsers";
}
This will return only the content and won't look for jsp
for example. (the reason you got 404
)
Upvotes: 0