How to use @Delete annotation in Vraptor + AJAX?

Asked

Viewed 359 times

4

In my Vraptor project I am trying to delete a record from a list without the page being reloaded. By the book I am following, AJAX is used so that the record removed "suma" without the whole page being loaded. The error below occurs when I try to use the @Delete annotation of vraptor.

HTTP Status 405 -

type: Status report

Description: The specified HTTP method is not allowed for the requested Resource.

My controller:

@Controller
@Path("/pessoa")
public class PessoaController {
[...]

@Delete("/remove/{codPessoa}")
public void remove(Long codPessoa) {
Pessoa pessoaEncontrada = repository.findById(codPessoa, 1);
    if (pessoaEncontrada != null) {
       repository.delete(pessoaEncontrada);
       result.nothing();
    } else
       result.notFound();
}}

On the JSP page:

<script src="${pageContext.request.contextPath}/js/jquery-2.1.4.min.js"></script>
<script>
    $("#pessoas.remove").on("click", function(event) {
        event.preventDefault();
        var pessoa = $(this).closest(".pessoa");
    });
    $.ajax({
        url : $(this).attr("href"),
        type : "POST",
        data : {_method : "DELETE"}
    }).done(function(data, textStatus, jqXHR) {
        pessoa.fadeOut();
    }).fail(function(jqXHR, textStatus, errorThrown) {
        alert("Cliente não foi alterado! " + errorThrown);
    });

[...]

<ul id=pessoas>
    <c:forEach items="${pessoaList}" var="pessoa">
        <li class="pessoa">${pessoa.codPessoa}-${pessoa.nomeFantasia}
            <a href="${linkTo[PessoaController].edita(pessoa.codPessoa, pessoa.codEmpresa)}"> -Editar</a> 
            <a class="remove" href="${linkTo[PessoaController].remove(pessoa.codPessoa)}"> -Remover</a>
        </li>
    </c:forEach>
</ul>

2 answers

1

I was able to make it work. But not by using @Delete. In fact, attribute "_method: 'DELETE'" is a parameter passed to Vraptor (according to what I read). Workaround: Replaces @Delete with @Path in the method and the deletion was performed. The mistake I was making is that in my script (js), the $.ajax was out of the Function, and the selector #people remove. should have a space between "#people" and ". remove" ("#people . remove"). Changing these structures the implementation worked.

  • After the changes in Function js, you can use the annotation Delete in place of Path in the controller method.

1

The problem is you’re trying to make a POST to a resource that only accepts DELETE, The solution is easy, you just need to correctly indicate which html method you want to use in the request:

$.ajax({
    url : $(this).attr("href"),
    method : "DELETE"
})

If you are using a jQuery version earlier than 1.9.0, instead of method use type same. Any doubt about the attributes of the ajax method, check the documentation.

  • Juliano... The attributes "type:'POST'" and "data: _method : "DELETE" is a resource for the action to work in any browser, according to the book I’m using for study. Even changing the way you proposed did not solve my problem. I believe it is some particularity of Vraptor. Thanks @Uliano

  • @raphaRezzi this is valid for the <form> to behave this way, but it is not so for js. You can check this here https://jsfiddle.net/qv8fw9g9/ by opening your Chrome console and clicking on each button, you will see that the way you suggest, a post is done, and as I suggested is a delete. Still, since you didn’t solve it, share the error to make it easier to help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.