I have this link, that what AJAX does is give a "like" to the response of a user.
Where it says ${respuesta.likes}
I show the likes of the answer.
The request does it to me correctly, that is, I add the "I like" to the answer, but the problem is that it does not change the "I like" that I show by screen in ${respuesta.likes}
.
<span th:text="${respuesta.likes}"></span>
<a id="like" class="far fa-thumbs-up fa-lg colorIcono" th:href="${'/respuesta/like/' + respuesta.respuestaSuya.id + '/' + respuesta.id}"></a>
Here I leave the AJAX method
$(document).ready(function() {
$("#like").click(function(event) {
$.ajax({
url: $(event.target).attr("href"),
type: "GET",
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success: function(data) {
console.log("ok");
}
});
event.preventDefault();
});
And the controller that executes the action
@GetMapping(value = "/respuesta/like/{id}/{idRes}")
public String darLike(@PathVariable("id")Long id,@PathVariable("idRes")Long idRes){
Usuario user = repoUsuario.usuarioPorId(id);
Respuesta res = repoRespuesta.respuestaPorId(idRes);
user.setPuntos(user.getPuntos() + 1);
res.setPuntos(res.getPuntos() + 1);
res.setLikes(res.getLikes() + 1);
repoUsuario.save(user);
repoRespuesta.save(res);
return "redirect:/respuesta/respuesta/" + res.getPostRespuesta().getId();
}