3

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
Jai
  • 74,255
  • 12
  • 74
  • 103
Federico Peppi
  • 57
  • 1
  • 1
  • 9

1 Answers1

5

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){...}
});
therealak12
  • 1,178
  • 2
  • 12
  • 26
Jai
  • 74,255
  • 12
  • 74
  • 103