使用AJAX通过WebApi Delete方法调用 [英] Call by WebApi Delete method using AJAX

查看:55
本文介绍了使用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方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆