删除请求ajax jquery不起作用

3

我想使用Ajax发送DELETE请求,但是它不起作用,我收到了一个内部错误,但我无法找到问题所在,你能帮助我吗?

以下是JavaScript的部分代码:

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

这里是我的控制器代码:

@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个回答

5

我在您的代码中发现以下问题:

  1. 如果您的响应是JSON对象,则使用dataType:'json'
  2. 但实际上,您的后端并没有生成json,而是一个boolean
  3. 您需要使用contentType:'application/json'
  4. 无需使用traditional:true

因此,我建议您使用以下代码:

$.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){...}
});

1
我使用了这个解决方案,它能够正常运行: - Federico Peppi
$.ajax({ url: 'http://localhost:8080/actors/' + movieId, type: 'DELETE', success: function(result) {...} - Federico Peppi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接