javascript - 删除表中的记录时出错 - 方法不允许“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");
}
解决方案
我已将 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");
}
推荐阅读
- javascript - 如何对 JSON 文件执行快速搜索?
- arrays - 匹配2个数组之间的值并排列在新数组中
- flask - Flask SQLAlchemy pymysql 警告:(1366 字符串值不正确)
- angular - 更新数据时,ag-grid cell-renderer 组件失去了可点击性
- java - 手动更改语言不适用于三星设备
- java - 如何捕捉字母并显示错误,除了整数?
- powershell - 为 Invoke-WebRequest 下载路径使用变量
- python - Python/numpy 点列表到黑白图像区域
- flask - Flask restx 返回 Class 而不是实际的返回字符串
- postgresql - 在 postgres 中合并时间戳间隔