Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call by WebApi Delete method using AJAX

I am using WebApi in a ASP.Net web application. I have a method in the controller called Delete and I want to access to this method by using jQuery's AJAX method. Below is my code:

[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>

The problem is that when I am using POST it is executing another method which starts as Post. Can anyone please help me with this?

like image 808
sandeep.mishra Avatar asked Mar 18 '15 13:03

sandeep.mishra


1 Answers

Assuming this is accessing a REST API, you need to send a DELETE request by setting the appropriate type in your $.ajax call:

$.ajax({
    url: "/Controller/Proposal/" + proposalId,
    type: "DELETE", // <- Change here
    contentType: "application/json",
    success: function() {
        bootbox.alert("Proposal deleted successfully.");
        ReloadGrid();
    },
    error: function() {
    }
});
like image 87
Rory McCrossan Avatar answered Oct 16 '22 17:10

Rory McCrossan