无法将删除功能从Ajax jQuery调用到Asp.net API函数 [英] Can't call delete function from Ajax jQuery to Asp.net API function

查看:60
本文介绍了无法将删除功能从Ajax jQuery调用到Asp.net API函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery 3.2.1.我正在尝试通过Ajax jQuery调用API.

I'm using jQuery 3.2.1. I'm trying to call API by Ajax jQuery.

我正在跟踪一个示例和类似这样的代码:

I'm following an example and code like this:

(() => {
    function delTest() {
        $.ajax({
            url: 'http://localhost:3413/api/person?ID=100',
            type: 'DELETE',
            dataType: 'json',
            data: { "": "Sourav Kayal" },
            success: function (data, textStatus, xhr) {
                console.log(data);
            },
            error: function (xhr, textStatus, errorThrown) {
                console.log('Error in Operation');
            }
        });
    }
})();

和API

   public class personController : ApiController  
    {  
        [HttpDelete]  
        public string Delete([FromUri] int ID, [FromBody] String name)  
        {  
            return "Delete Operation" + "ID:- " + ID + "Name:- " + name;  
        }  
    }


protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);

        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

但是,它现在不起作用.

However, it's not working now.

返回错误:找不到404

Returns an error: 404 Not found

推荐答案

我正在使用下面的代码,并且在我的机器上可以正常工作.我认为您需要在URL中添加方法名称delete,它的外观应为" http://localhost:3413/api/person/Delete?ID = 100 "

I am using below code and its working fine on my machine. I think you need to add method name delete in the URL, it should look like "http://localhost:3413/api/person/Delete?ID=100"

<script type="text/javascript">

        function delTest() {

        $.ajax({
            url: 'http://localhost:3413/person/Delete?ID=100',
            type: 'DELETE',
            dataType: 'json',
            data: { name: "Sourav Kayal" },
            success: function (data, textStatus, xhr) {
                console.log(data);
            },
            error: function (xhr, textStatus, errorThrown) {
                console.log('Error in Operation');
            }
        });
    }
</script> 

<input type="button" name="delete" onclick="delTest()" value="makeCall"/>

和API代码:

  [System.Web.Http.HttpDelete]
        public string Delete([FromUri] int ID, [FromBody] String name)
        {
            return "Delete Operation" + "ID:- " + ID + "Name:- " + name;
        }

这篇关于无法将删除功能从Ajax jQuery调用到Asp.net API函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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