【问题标题】:delete request ajax jquery don't function删除请求ajax jquery不起作用
【发布时间】:2016-01-22 12:08:57
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: javascript jquery ajax controller delete-record


    【解决方案1】:

    我在您的代码中看到的问题:

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

    【讨论】:

    • 我使用了这个解决方案,它的功能:
    • $.ajax({ url: 'localhost:8080/actors' + movieId, type: 'DELETE', 成功: function(result) {...}
    猜你喜欢
    • 2012-11-28
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多