When I utilize a literal stringified JSON object, everything runs smoothly as anticipated
Index.cshtml (with literal ajax string $.ajax OPERATES)
var searchObject = new Object();
searchObject.Thing1 = '42';
searchObject.Thing2 = '43';
searchObject.Thing3 = '44';
var searchSerialized = JSON.stringify(searchObject);
alert(searchSerialized);
$.ajax(
{
url: '/api/auditApi',
type: "Post",
contentType: "application/json; charset=utf-8",
data: '{\'Thing1\':\'42\',\'Thing2\':\'43\',\'Thing3\':\'44\'}',
dataType: 'json',
success: function (result) {
$(".kendoAudit").data("kendoGrid").dataSource = new kendo.data.DataSource({ data: result });
$(".kendoAudit").data("kendoGrid").dataSource.read();
$(".kendoAudit").data("kendoGrid").refresh();
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Status: ' + xhr.status + ', Error Thrown: ' + thrownError);
}
});
However, when I opt for a serialized object generated inline, it receives null passed into the post handler
Index.cshtml (with object created inline and sent to $.ajax FAILS)
var searchObject = new Object();
searchObject.Thing1 = '42';
searchObject.Thing2 = '43';
searchObject.Thing3 = '44';
var searchSerialized = JSON.stringify(searchObject);
alert(searchSerialized);
$.ajax(
{
url: '/api/auditApi',
type: "Post",
contentType: "application/json; charset=utf-8",
data: searchSerialized, //I have tried concatenating with + '' also
dataType: 'json',
success: function (result) {
$(".kendoAudit").data("kendoGrid").dataSource = new kendo.data.DataSource({ data: result });
$(".kendoAudit").data("kendoGrid").dataSource.read();
$(".kendoAudit").data("kendoGrid").refresh();
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Status: ' + xhr.status + ', Error Thrown: ' + thrownError);
}
});
Controller
public HttpResponseMessage Post([FromBody]string value)
{
try
{
var results = JsonConvert.DeserializeObject<dynamic>(value);
}
catch (Exception ex)
{
// throws because value is null.
}
}
I am certain that there must be something glaringly obvious that I am overlooking in this conversion process.
The debug line: alert(searchSerialized);
showcases the expected output