首页 > 解决方案 > 删除表中的记录时出错 - 方法不允许“405(方法不允许)”

问题描述

我想使用 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");
        }

标签: javascriptc#ajaxasp.net-core

解决方案


我已将 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");
        }

推荐阅读