简体   繁体   English

删除表中的记录时出错 - 方法不允许“405(方法不允许)”

[英]getting error while deleting record in the table - method not allowed "405 (Method Not Allowed)"

i want to delete record using ajax call but im getting error method not allowed.我想使用 ajax 调用删除记录,但我得到了不允许的错误方法。 405 error. 405 错误。

code代码

HTML HTML

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

JS 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# code代码

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

i have changed both ajax and controlled code to GET and HttpGet, Now its working.我已将 ajax 和受控代码更改为 GET 和 HttpGet,现在可以正常工作了。

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

C# C#

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

I test my code,and I find HTTPDelete and HttpPost can work.我测试了我的代码,我发现 HTTPDelete 和 HttpPost 可以工作。

Here is a demo for HTTPDelete:这是 HTTPDelete 的演示:

View:看法:

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

controller:控制器:

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

Result:结果: 在此处输入图片说明

Here is a demo for HTTPPost:这是 HTTPPost 的演示:

View:看法:

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

controller:控制器:

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

Result:结果: 在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM