0

my class api:

[Route("Api")]
[ApiController]
public class ApiUsersController : ControllerBase
{
    [HttpPost]
    [Route("Users/demo")]
    public ActionResult demo([FromBody]model data)
    {
       return  "OK";
    }
}

my script jquery:

var model = {key: "123"}
$.ajax({
    type: 'POST',
    url: 'Api/Users/demo',
    data: JSON.stringify(model),
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    async: true,
    ...
});

I can't call api in project, please help me

how to resold problem

Felix
  • 9,248
  • 10
  • 57
  • 89
kai
  • 15
  • 2

2 Answers2

0

Change public ActionResult demo(model data) To public ActionResult demo( [FromBody] model data)

Works in my case: enter image description here

enter image description here

SIbghat
  • 281
  • 3
  • 5
0

Change your contentType and try:

var model = {key: "123"}
$.ajax({
    type: 'POST',
    url: 'Api/Users/demo',
    data: model,
    dataType: 'json',
    contentType: 'application/x-www-form-urlencoded',
    //async: true,
    ...
});
Moritz Ringler
  • 9,772
  • 9
  • 21
  • 34