【发布时间】:2017-09-19 18:40:47
【问题描述】:
我有以下 ajax 请求,我正在尝试将 JSON 对象发送到服务器:
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);
}
});
}
根据this post 中的最佳答案,我在“data”属性周围添加了引号,但我收到一条错误消息,指出“subscriptionJSON”未被识别。我尝试使用示例字符串进行测试,如帖子中所做的那样:数据:"{'foo':'foovalue', 'bar':'barvalue'}", 但是当控制器获取 subscriptionJson 对象的参数时,它为空。
通过 POST 请求将 JSON 对象发送到 ASP .NET MVC 控制器的官方方法是什么?
【问题讨论】:
标签: jquery json ajax asp.net-mvc