0

I have a web api method like below:

    /// <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:

            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(); and test this out, it works. Except the param string[] items is always empty.

If I look into the network tab of Google Chrome I can see the items in my form data:

enter image description here

What am I missing here?

7
  • What is the value of values? And set contentType: 'application/json' Commented Jun 12, 2017 at 11:03
  • @StephenMuecke view update. Commented Jun 12, 2017 at 11:04
  • Also data: JSON.stringify({ items: values }) Commented Jun 12, 2017 at 11:05
  • Now its null instead of an empty array Commented Jun 12, 2017 at 11:07
  • @StephenMuecke ok now its solved. I have created a new class model with one property called Items of type string[] Commented Jun 12, 2017 at 11:20

3 Answers 3

3

Change to this: As JSON.stringify turns a Javascript object into JSON text and stores that JSON text in a string.

  $.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');
        });
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of writing data: { items: values }, you should do data: JSON.stringify({ 'items': values }).

This is how ajax works with arrays.

Comments

0

Thanks to @Stephen Muecke I first added some changes to my ajax call:

$.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:

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

And now its working!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.