0

This is a question that has come up before, and the usual answer seems to be:

  • to add [FromBody] to the parameter
  • to add content type of application / json in the post

but neither of these have helped.

When the ajax post happens, it hits the web api action, but the parameter is null

I have the following server side code

[HttpPost]
    [Route("SafeFloatTopUp")]
    public void SafeFloatTopUp([FromBody] SafeFloatDenomination d )
    {
        //d is null
    }


 public class SafeFloatDenomination
{
    public SafeFloatDenomination();

    public string Denomination { get; set; }
    public decimal Value { get; set; }
}

and this is called from the following client code:

  var d =  { Denomination: "1p", Value:  2 };
            bankingApi.client.topUp(d);

var topUp = function (denomination) {
    $.ajax({
        url: '/portalframework/BackOffice/Banking/Banking/SafeFloatTopup',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: denomination
    });
};
jazza1000
  • 4,099
  • 3
  • 44
  • 65

2 Answers2

0

Found the answer on the following stackoverflow question

I needed to use

JSON.stringify(denomination)
Community
  • 1
  • 1
jazza1000
  • 4,099
  • 3
  • 44
  • 65
0

You don't need to specify the content type - the default content type of 'application/x-www-form-urlencoded; charset=UTF-8' will handle javascript objects without needing to convert them to strings:

$.ajax({
    url: '/portalframework/BackOffice/Banking/Banking/SafeFloatTopup',
    type: 'POST',
    data: denomination
});
psantiago
  • 695
  • 6
  • 16