【问题标题】:getting error while deleting record in the table - method not allowed "405 (Method Not Allowed)"删除表中的记录时出错 - 方法不允许“405(方法不允许)”
【发布时间】:2020-10-27 18:53:13
【问题描述】:

我想使用 ajax 调用删除记录,但我得到了不允许的错误方法。 405 错误。

代码

HTML

 <button class="btn btn-danger" onclick="DeleteTrip(@item.TripId)">Delete</button>

JS

var DeleteTrip = function (TripId) {

        var ans = confirm("Do you want to delete item with Item Id: " + TripId);

        if (ans) {
            $.ajax({
                type: "POST",
                url: "/TripsReport/Delete/" + TripId,
                success: function () {
                    window.location.href = "/TripsReport/Index";
                }
            })
        }
    }

c#代码

 [HttpPost]
        public IActionResult Delete(int id)
        {
            tripsService.DeleteTrips(id);
            return RedirectToAction("Index");
        }

【问题讨论】:

  • 网址不应该是 "/TripsReport/Delete?id=" + TripId,
  • 但是调试点没有命中Delete方法。
  • 你指定路由了吗?
  • 是的,我已经指定了路由,这是 .net core 。我可以知道路线在哪里。
  • 我也认为不需要成功部分,因为您的服务器代码中已经有 RedirectToAction()。

标签: javascript c# ajax asp.net-core


【解决方案1】:

我测试了我的代码,我发现 HTTPDelete 和 HttpPost 可以工作。

这是一个 HTTPDelete 的演示:

查看:

<button class="btn btn-danger" onclick="DeleteTrip(1)">Delete</button>

    @section scripts{
    
        <script>
            function DeleteTrip (TripId) {
    
                var ans = confirm("Do you want to delete item with Item Id: " + TripId);
    
                if (ans) {
                    $.ajax({
                        type: "DELETE",
                        url: "/TripsReport/Delete",
                        data: {
                            id: TripId
                        },
                        success: function (data) {
                            window.location.href = "/TripsReport/Index";
                        }
                    })
                }
            }
        </script>
    }

控制器:

[HttpDelete]
        public IActionResult Delete(int id)
        {
            return Ok();
        }

结果:

这是 HTTPPost 的演示:

查看:

<button class="btn btn-danger" onclick="DeleteTrip(1)">Delete</button>

    @section scripts{
    
        <script>
            function DeleteTrip (TripId) {
    
                var ans = confirm("Do you want to delete item with Item Id: " + TripId);
    
                if (ans) {
                    $.ajax({
                        type: "POST",
                        url: "/TripsReport/Delete",
                        data: {
                            id: TripId
                        },
                        success: function (data) {
                            window.location.href = "/TripsReport/Index";
                        }
                    })
                }
            }
        </script>
    }

控制器:

[HttpPost]
        public IActionResult Delete(int id)
        {
            return Ok();
        }

结果:

【讨论】:

    【解决方案2】:

    我已将 ajax 和受控代码更改为 GET 和 HttpGet,现在可以使用了。

     if (ans) {
                $.ajax({
                    type: "GET",
                    url: "/TripsReport/Delete/" + TripId,
                        success: function () {
                        window.location.href = "/TripsReport/Index";
                    }
                })
            }
    

    C#

     [HttpGet]
            public IActionResult Delete(int id)
            {
                tripsService.DeleteTrips(id);
                return RedirectToAction("Index");
            }
    

    【讨论】:

    • 获取可以被浏览器缓存,所以要小心。帖子应该可以工作,尽管我自己看不到问题所在。
    • 您是否尝试了 [httpDelete] 命令,也许 Post 需要正文。
    • @RichardHubley 我尝试使用 httpDelete,它的说法不允许
    • 当 c# 有 [HttpDelete] 而 ajax 有 type = "DELETE" 时,你是否再次收到 405 错误?
    • @RichardHubley。是的,我的方法不允许用于 httpdelete 和 Delete。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 2014-06-21
    相关资源
    最近更新 更多