Spring Boot中的Ajax删除方法 [英] Ajax delete method in spring boot

查看:493
本文介绍了Spring Boot中的Ajax删除方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After looking on different questions on forums talking about the same problem, I still don't know how to resolve my Request method 'DELETE' not supported

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

An Ajax method call is fired when a user in the client-side press a button, this method retrieves the sportId wrapped in an Ajax call and send to spring controller to Delete method.

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;
    }
}

即使我在网址中发送ID,我仍然会收到相同的错误

even if I send id in url, I still get the same error

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

推荐答案

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

You are sending request to incorrect url. You declared path for the controller as /sports-actions/{id}, but actually sending it to /sports-actions with json body.

正确的代码:

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

这篇关于Spring Boot中的Ajax删除方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆