Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WEB API - FromBody parameter is not working

I'm having an issue with my body parameter being always null. Here's the controller:

    [HttpPost]
    public async void Assign([FromUri] int[] sectionIds, [FromBody] int[] ids) ...

The URI parameter is filled out. However, The Body parameter is always empty.

Here's the ajax request:

    return $.ajax(
        {
            url: api_url,
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({
                "ids": ids
            })
        })

Here's what I see in the payload on in my network monitor.

    {"ids":[597,1893]}:

What's could be the problem here?

EDIT: I'm using an API Controller

like image 631
Machinegon Avatar asked Oct 17 '25 13:10

Machinegon


1 Answers

It seems that URL is not in correct place. Could you try the following with contentType?

$.ajax({
   url: API_URL,
   contentType: "application/json",
   method: "POST",
   data: JSON.stringify({"ids": ids}, 
   success: function(result) { return result; }
});
like image 128
Win Avatar answered Oct 20 '25 05:10

Win



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!