Currently working on a login page using Spring MVC on the server side and JS/Ajax on the client side. I'm facing an issue where the server code executes but does not return anything.
<script type="text/javascript">
$(function() {
$(".loguser").click(function() {
var user = $("#login").val();
var pass = $("#pass").val();
$.ajax({
method: "POST",
url : "${prefix}loginUser",
data : "username=" + user + "&password=" + pass,
dataType : "json",
success: function(data){
if (data.res == "YES") alert("ok");
else alert("NOPE");
}
});
})
})
</script>
<table id="userData" class="center">
<tr id="usernametr">
<th><label for="user">Nombre de usuario: </label></th>
<th><input id="login" type="text" name="login" value=""
placeholder="Name" required /></th>
</tr>
<tr>
<th><br /></th>
</tr>
<tr>
<th><label for="pass">Clave de usuario: </label></th>
<th><input id="pass" type="password" name="pass" value=""
placeholder="Password" required /></th>
</tr>
<tr>
<th><button class="loguser">Acceder</button></th>
<th><input type="button" name="lost"
value="He perdido mi clave" /></th>
</tr>
</table>
HomeController.java:
@RequestMapping(value = "/loginUser")
@ResponseBody
@Transactional
public String loginUser(@RequestParam("username") String username,
@RequestParam("password") String pass, HttpServletRequest request, Model model) {
logger.info("Trying to log in {} {}", username, pass);
if (username.length() > 3) {
logger.info("ok");
return new String("[\"res\": \"YES\"]");
} else {
logger.warn("nope");
return new String("[\"res\": \"NOPE\"]");
}
}
Also tried returning EntityResponse, but no changes were observed. The Spring console prints the logger info or warn statements, but Firefox's JavaScript console does not show any output.