简体   繁体   English

使用jQuery Ajax的ASP.NET MVC Web API删除调用,数据参数不起作用

[英]ASP.NET MVC Web API Delete call using jQuery Ajax with data params not working

I have a web api method like below: 我有一个如下所示的Web api方法:

    /// <summary>
    /// DELETE: api/ftpapi/custom/deletefiles
    /// </summary>
    /// <param name="items"></param>
    /// <returns></returns>
    [HttpDelete]
    [ResponseType(typeof(void))]
    public IHttpActionResult DeleteFiles(string[] items)
    {
        return Ok();
    }

In my view I call this method using Ajax: 在我看来,我使用Ajax调用此方法:

            var values = $('input:checkbox[name=items]:checked').map(function () {
            return this.value;
            }).get();

            $.ajax({
                url: uri_api + '/custom/deletefiles',
                method: "DELETE",
                data: { items: values }
            }).done(function (data) {
                    location.reload(true);
                })
            .fail(function (jqXHR, textStatus, err) {
                console.log('Error: ' + err);
            })
            .always(function () {
                $('#loader').fadeOut(200);
                $('body').removeClass('loader-in');
            });

When I place a breakpoint at the return Ok(); 当我在return Ok();处放置一个断点时return Ok(); and test this out, it works. 并进行测试,就可以了。 Except the param string[] items is always empty. 除了param string[] items始终为空。

If I look into the network tab of Google Chrome I can see the items in my form data: 如果查看Google Chrome浏览器的“网络”标签,则可以在表单数据中看到这些项目:

在此处输入图片说明

What am I missing here? 我在这里想念什么?

Change to this: As JSON.stringify turns a Javascript object into JSON text and stores that JSON text in a string. 更改为:当JSON.stringify将Javascript对象转换为JSON文本并将该JSON文本存储在字符串中。

  $.ajax({
            url: uri_api + '/custom/deletefiles',
            contentType: "application/json; charset=utf-8",
            method: "DELETE",
            data: JSON.stringify({ items: values })
        }).done(function (data) {
                location.reload(true);
            })
        .fail(function (jqXHR, textStatus, err) {
            console.log('Error: ' + err);
        })
        .always(function () {
            $('#loader').fadeOut(200);
            $('body').removeClass('loader-in');
        });

Instead of writing data: { items: values } , you should do data: JSON.stringify({ 'items': values }) . 而不是编写data: { items: values } ,您应该执行data: JSON.stringify({ 'items': values })

This is how ajax works with arrays. 这就是ajax与数组一起工作的方式。

Thanks to @Stephen Muecke I first added some changes to my ajax call: 感谢@Stephen Muecke我首先对ajax调用进行了一些更改:

$.ajax({
     url: uri_api + '/custom/deletefiles',
     method: "DELETE",
     data: JSON.stringify({ 'items': values }), // Added Stringify
     contentType: 'application/json' // Added content type
});

Ater that I have created a new class model with one property: 我曾经用一个属性创建了一个新的类模型:

public class FtpFilesModel
{
    public string[] Items { get; set; }
}

Then the last thing to do is changing the method property to the new created class: 然后,最后要做的就是将method属性更改为新创建的类:

[HttpDelete]
[ResponseType(typeof(void))]
public IHttpActionResult DeleteFiles(FtpFilesModel files)
{
    return Ok();
}

And now its working! 现在可以正常工作了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM