If you are using jQuery’s ajax function to post raw JSON string to your APIs, you might get some problem when your API does not receive the correct content, or in the case of ASP.NET Web API and parameter binding, you may get null input parameters in your controller.
The JavaScript can be as simple as this.
1 2 3 4 5 6 |
$.ajax('/api/test', { type: 'post', data: 'inputparameter', dataType: 'json', success: function(data) { alert('Receive: ' + data); } }); |
And the post action of the Test API controller be something like below.
1 2 3 4 |
public string Post([FromBody]string input) { return input; } |
The post action will not get “inputparameter” as the value of variable input; instead we get a null value.
Read More