Hi, I have trouble calling a controller using ajax, what I want to do is go to another page using ajax.
function validar(){
var codigo= $('#codigo').val();
$.ajax({
type : "GET",
url : "/host/validar",
data: {
"codigo" : codigo
},
success: function(result) {
alert("success");
},
error : function(e) {
alert("error");
}
});
}
the html with thymeleaf
<div class="x_content">
<section class="text-center">
<h5><span>Ingrese codigo</span></h5>
<form id="formValidar">
<div>
<input type="text" class="form-control" placeholder="codigo" required="" id="codigo" name="codigo" />
</div>
<br>
<div>
<button class="btn btn-dark" type="button" onclick="validar()">Validar</button>
</div>
</form>
</section>
</div>
and in my controller
@RequestMapping(value = "/validar", method = RequestMethod.GET)
public @ResponseBody String validar(
@RequestParam("codigo") String codigo){
System.out.println("entro sdgsdgdsg");
System.out.println("codigo: "+codigo);
Usuario usuario = usuarioService.buscarPorCodigo(codigo);
if (usuario == null){
return "pagina-validar";
}
return "nueva-pagina";
}
I have added the @Controller annotation in the class, and I have another example almost the same in another class and if it works, but it does not even enter the method.