delete request ajax jquery don’t function Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of delete request ajax jquery don’t function without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m trying to do a DELETE request with Ajax, but it doesn’t function,I receive an internal error, but i can’t see the problem, can you help me?

this is the partial code of javascript:

$.ajax({
    url: 'http://localhost:8080/actors/remover',
    type: 'DELETE',
    data: JSON.stringify(movie),
    traditional:true,
    dataType: 'json',
    success: function(result) {...},
    error: function(result){...}
});

and here the code of my controller:

@RequestMapping(value = "/actors/remover", method = RequestMethod.DELETE)//TODO, elimina un attore dal db
public boolean remove(@PathVariable("movie") int movie) {
    System.out.println("Attori da cancellare");
    serv.deleteActors(movie);
    return true;
}//remove

Answer

Problems i see in your code:

  1. dataType:'json' is used if you get your response as a json object.
  2. At the backend you are not producing json at all but there is a boolean.
  3. You have to use contentType:'application/json'.
  4. And there is no need to use traditional:true.

So i suggest you to use this:

$.ajax({
    url: 'http://localhost:8080/actors/remover',
    type: 'DELETE',
    data: {movie:movie}, //<-----this should be an object.
    contentType:'application/json',  // <---add this
    dataType: 'text',                // <---update this
    success: function(result) {...},
    error: function(result){...}
});
We are here to answer your question about delete request ajax jquery don’t function - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji