简体   繁体   English

HTTP状态405-调用删除请求时,请求方法'GET'不支持

[英]HTTP Status 405 - Request method 'GET' not supported occurs when calling a delete request

I am trying to implement delete action for the each datatable row by invoking a bootstrap modal. 我正在尝试通过调用引导程序模式为每个数据表行实现删除操作。 And then sending a delete request to the controller with the parameter of username. 然后使用用户名参数向控制器发送删除请求。

   $(document).ready(function () {
    $('#usersTable').DataTable({
        "sAjaxSource": "users/list",
        "sAjaxDataProp": "",
        "aoColumns": [
            {"mData": "username"},
            {"mData": "firstName"},
            {"mData": "lastName"},
            {"mData": "status"},
            {"mData": "role.roleName"},
            {
                mRender: function (data, type, row) {

                    var linkDel = "<a href=\"#\" onclick=\"fillDeleteFrm(\'username\')\">Delete</a>";
                    linkDel = linkDel.replace("id", row.id);
                    linkDel = linkDel.replace("name", row.name);
                    return linkDel;
                }
            }
        ]
    })
});

   function fillDeleteFrm(username) {
    $('#delete-username').val(username);
    $('#userDeleteModal').modal('toggle');
    return false;
}

The thymeleaf form that calls the spring controller : 称为弹簧控制器的百里香形式:

        <div id="userDeleteModal">
            <form id="systemUserForm" th:action="@{/userdelete}" th:method="delete">
                    <input type="hidden" id="delete-username" name="username" th:value="*{username}">
                        <button type="submit" class="btn btn-primary">Delete</button>
                    </div>
                </form>

The corresponding spring controller : 对应的弹簧控制器:

    @ResponseBody
    @RequestMapping(value="/userdelete", method = RequestMethod.DELETE)
    public ModelAndView deleteEmployee(@RequestParam String username) {
    User user = userService.findUserByUserName(username);
    userService.deleteUser(user);
    return new ModelAndView("users");
  }

Even though I call the delete request method the mapping does not route to this controller, alerting 即使我调用了delete request方法,映射也不会路由到该控制器,提醒

HTTP Status 405 - Request method 'GET' not supported

What would I have done wrong? 我做错了什么? Thanks in advance. 提前致谢。

Browsers do not support DELETE method in forms. 浏览器不支持表单中的DELETE方法。 With th:method="delete" , Thymleaf adds a hidden _method form field. 通过th:method="delete" ,Thymleaf添加了一个隐藏的_method表单字段。

<input type="hidden" name="_method" value="delete">

If you add [HiddenHttpMethodFilter][1] filter in your application, it will convert the post request to a delete request based on the _method parameter. 如果在应用程序中添加[HiddenHttpMethodFilter][1]过滤器,它将根据_method参数将发布请求转换为删除请求。 Try adding the following in your web.xml file. 尝试在web.xml文件中添加以下内容。

If you are using spring boot, the filter is automatically included. 如果您使用的是弹簧靴,则将自动包含过滤器。 But in a plain spring web mvc app, you'll have to add the filter manually in web.xml file. 但是在普通的Spring Web MVC应用程序中,您必须在web.xml文件中手动添加过滤器。 This filter is probably missing in your app. 您的应用中可能缺少此过滤器。 That is why the GET requested is not being converted to a DELETE request. 这就是为什么未将请求的GET转换为DELETE请求的原因。

<filter>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>     
</filter-mapping>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTTP状态405 - 不支持请求方法“GET” - HTTP Status 405 - Request method 'GET' not supported 获取“405 - 调用”method = DELETE时不支持请求方法&#39;GET&#39; - Getting “405 - Request method 'GET' not supported when calling” method=DELETE HTTP状态405-请求方法&#39;GET&#39;不支持MVCspring - HTTP Status 405 - Request method 'GET' not supported MVCspring HTTP 状态 405 - 不支持请求方法“PUT” - HTTP Status 405 - Request method 'PUT' not supported HTTP状态405-文件上传时不支持请求方法“ POST” - HTTP Status 405 - Request method 'POST' not supported when file uploading HTTP状态405-在Spring MVC中执行返回“重定向:*。*”时,不支持请求方法“ GET” - HTTP Status 405 - Request method 'GET' not supported when doing return “redirect:*.*” in spring mvc 编辑时:HTTP状态405-请求方法&#39;POST&#39;不支持 - While editing: HTTP Status 405 - Request method 'POST' not supported HTTP状态405-请求方法&#39;POST&#39;不支持-jQuery.post() - HTTP Status 405 - Request method 'POST' not supported - jQuery.post() Spring MVC - HTTP 状态 405 - 不支持请求方法“POST” - Spring MVC - HTTP Status 405 - Request method 'POST' not supported SpringMVC HTTP状态405-请求方法&#39;POST&#39;不支持 - SpringMVC HTTP Status 405 - Request method 'POST' not supported
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM