本文介绍了使用AJAX通过WebApi Delete方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在ASP.Net Web应用程序中使用WebApi.我在控制器中有一个名为 Delete 的方法,我想使用jQuery的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>问题是当我使用 POST 时,它正在执行另一个以Post开始的方法.谁能帮我这个忙吗?
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?
推荐答案假设这正在访问REST API,则需要通过设置相应的 type 发送 DELETE 请求>在您的 $.ajax 呼叫中:
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() { } });更多推荐
使用AJAX通过WebApi Delete方法调用
发布评论