使用AJAX通过WebApi调用Delete方法

2024-05-15

我在 ASP.Net Web 应用程序中使用 WebApi。我在控制器中有一个名为Delete我想通过使用 jQuery 的 AJAX 方法来访问此方法。 下面是我的代码:

[Authorize]
public int Delete(int proposalId)
{
    // logic here...     
}
$.ajax({
    url: "/Controller/Proposal/" + proposalId,
    type: "Post",
    contentType: "application/json",
    success: function() {
        bootbox.alert("Proposal deleted successfully.");
        ReloadGrid();
    },
    error: function() {
    }
});
RouteTable.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "controller/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"></modules>
</system.webServer>

问题是当我使用时POST它正在执行另一个以 Post 开头的方法。谁能帮我解决这个问题吗?


假设这是访问 REST API,您需要发送DELETE通过设置适当的请求type在你的$.ajax call:

$.ajax({
    url: "/Controller/Proposal/" + proposalId,
    type: "DELETE", // <- Change here
    contentType: "application/json",
    success: function() {
        bootbox.alert("Proposal deleted successfully.");
        ReloadGrid();
    },
    error: function() {
    }
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用AJAX通过WebApi调用Delete方法 的相关文章

随机推荐