使用AJAX通过WebApi Delete方法调用

编程入门 行业动态 更新时间:2024-10-24 09:30:48
本文介绍了使用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方法调用

本文发布于:2023-11-16 14:25:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605096.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   AJAX   WebApi   Delete

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!