Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete request ajax jquery don't function

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
like image 454
Federico Peppi Avatar asked Dec 11 '25 00:12

Federico Peppi


1 Answers

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){...}
});
like image 95
Jai Avatar answered Dec 12 '25 14:12

Jai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!