I have the following ajax request in which I'm trying to send a JSON Object to the server:
function sendData(subscriptionJson) {
$.ajax({
type: "POST",
url: '@Url.Action("SubscribeSecurities", "Subscription")',
data: "{'subscriptions': subscriptionJson}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log("success response: " + response.responseText);
alert("Hello: " + response.Name + " .\nCurrent Date and Time: " + response.DateTime);
},
failure: function (response) {
console.log("failure response: " + response.responseText);
alert(response.responseText);
},
error: function (response) {
console.log("error response: " + response.responseText);
alert(response.responseText);
}
});
}
Based on the top answer in this post I added quotes around the "data" attribute, but I'm getting an error saying that the "subscriptionJSON" isn't being recognized. I tried testing with a sample string as is done in the post: data: "{'foo':'foovalue', 'bar':'barvalue'}",
but when the controller gets the parameter of the subscriptionJson object it is null.
What is the official way to send a JSON object via a POST request to an ASP .NET MVC controller?