[英]getting error while deleting record in the table - method not allowed "405 (Method Not Allowed)"
我想使用 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";
}
})
}
}
代碼
[HttpPost]
public IActionResult Delete(int id)
{
tripsService.DeleteTrips(id);
return RedirectToAction("Index");
}
我已將 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 和 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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.