简体   繁体   中英

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. 405 error.

code

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# 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.

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

I test my code,and I find HTTPDelete and HttpPost can work.

Here is a demo for 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:

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: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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