delete request ajax jquery don't function

25.1k Views Asked by At

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
1

There are 1 best solutions below

2
On BEST 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){...}
});