I came through old Q&A like jQuery posts null instead of JSON to ASP.NET Web API (on this site),
http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/ or
https://weblog.west-wind.com/posts/2012/May/08/Passing-multiple-POST-parameters-to-Web-API-Controller-Methods...
I tried all the hints and solutions, but I still have no values in my controller.
Here is my code:
Client HTML file :
var coup = { Token: token, MotPropose: RecupMot() };
var coupJSON = JSON.stringify(coup);
$.ajax({
type: 'POST',
url: urlJeu,
dataType: 'json',
data: coupJSON,
success: function (reponseJSON) {
/* CODE HERE */
},
});
Which sends correctly the following JSON (says Fiddle):
{"Token":103,"MotPropose":"ravoi"}
(BTW, I'm concerned about the missing " around the int 103, no?)
Server C# controller and object:
public object Post([FromBody]CoupMeliMelo coup)
{
return Ok(jeu.JouerLeCoup(coup));
}
public class CoupMeliMelo : ICoup
{
public string MotPropose { get; set; }
public int Token { get; set; }
}
Then, when the JSON comes to the controller, the object coup is created, but MotPropose is null and Token is 0.
I'm stuck! Thanks for your help.
contentType: "application/json; charset=utf-8",to your ajax call.