使用AJAX通过WebApi Delete方法调用

san*_*hra 5 asp.net jquery asp.net-web-api jquery-ajaxq

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

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

问题是,当我使用POST它时,正在执行另一个以Post开头的方法.有人可以帮我这个吗?

Ror*_*san 6

假设这是访问REST API,您需要DELETE通过type$.ajax呼叫中设置适当的方式来发送请求:

$.ajax({
    url: "/Controller/Proposal/" + proposalId,
    type: "DELETE", // <- Change here
    contentType: "application/json",
    success: function() {
        bootbox.alert("Proposal deleted successfully.");
        ReloadGrid();
    },
    error: function() {
    }
});
Run Code Online (Sandbox Code Playgroud)