【问题标题】:Ajax delete method in spring bootspring boot中的ajax删除方法
【发布时间】:2017-12-14 13:46:11
【问题描述】:

在论坛上看了不同的问题谈论同样的问题后,我仍然不知道如何解决我的请求方法'DELETE'不支持

客户端中的用户按下按钮时会触发 Ajax 方法调用,该方法检索包装在 Ajax 调用中的 sportId 并发送到 spring 控制器删除方法。

Ajax 方法:

function removeRow(link) { 
    var sportId = link.getAttribute("data-sport-id");

    $.ajax({
        type : "DELETE",
        url : "/sports-actions",
        data: {id : sportId},
        contentType: "application/json",
        dataType : 'json',
        success: function (result) {       
               console.log(result);                
        },
        error: function (e) {
            console.log(e);
        }
   })   

}

弹簧控制器:

@RestController
@RequestMapping("/sports-actions")
public class SportController {  

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Object deleteSport(@PathVariable("id") String id) {
        return null;
    }
}

编辑:

即使我在 url 中发送 id,我仍然会收到相同的错误

Ajax 代码:

 $.ajax({
        type : 'DELETE',
        contentType: "application/json",
        url : "/sports-actions?id="+sportId,
        dataType : 'json',
        success: function (result) {       
               console.log(result);                
        },
        error: function (e) {
            console.log(e);
        }
   }) 

【问题讨论】:

  • 检查您的 AJAX 请求,您没有在请求 url 中传递 id。相反,您发送的是正文。 url 可以是 -> url: '/sports-actions/' + sportId.
  • 我仍然遇到同样的错误,我以前也这样做过。
  • 查找@PathVariable@RequestParam 之间的区别。在您的编辑案例场景中,使用@ReqeustParam
  • 哦,我真的忘记了,这是解决方案的一部分。它有效

标签: jquery ajax spring


【解决方案1】:

您正在向错误的网址发送请求。您将控制器的路径声明为 /sports-actions/{id},但实际上使用 json 正文将其发送到 /sports-actions

正确代码:

$.ajax({
    type : "DELETE",
    url : "/sports-actions/" + sportId,
    success: function (result) {       
           console.log(result);                
    },
    error: function (e) {
        console.log(e);
    }
});

【讨论】:

    【解决方案2】:

    请查看@RequestParam@PathVariable 之间的区别。

    如果你想使用@ReqeustParam:

    $.ajax({
        type : "DELETE",
        url : "/sports-actions",
        data: {"id" : sportId},
        contentType: "application/json",
        dataType : 'json',
        success: function (result) {       
               console.log(result);                
        },
        error: function (e) {
            console.log(e);
        }
    })   
    
    @RestController
    @RequestMapping("/sports-actions")
    public class SportController {  
    
        @RequestMapping(method = RequestMethod.DELETE)
        public Object deleteSport(@RequestParam("id") String id) {
            return null;
        }
    }
    

    如果你想使用@PathVariable

    $.ajax({
        type : "DELETE",
        url : "/sports-actions/" + sportId,
        contentType: "application/json",
        dataType : 'json',
        success: function (result) {       
               console.log(result);                
        },
        error: function (e) {
            console.log(e);
        }
    }) 
    
    
    
    @RestController
    @RequestMapping("/sports-actions")
    public class SportController {  
    
        @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
        public Object deleteSport(@PathVariable("id") String id) {
            return null;
        }
    }
    

    PS:遵循Restful 最佳实践总是好的。 see here

    【讨论】:

    • 我专注于我的 Ajax 调用,我完全忘记了这个重要的部分,很好
    猜你喜欢
    • 2020-07-18
    • 2019-04-29
    • 2022-12-09
    • 2021-06-17
    • 1970-01-01
    • 2021-12-13
    • 2019-01-30
    • 2018-09-22
    • 2020-03-20
    相关资源
    最近更新 更多