[英]NULL values while passing JSON data to controller via AJAX
[英]AJAX Passing JSON to Controller as String Returns Null
我有以下AJAX调用,经过简化以尝试查明问题所在:
$('#userUpdateForm').submit(function (e) {
$.ajax({
type: "POST",
url: '@Url.Action("submitForm", "Home")',
data: JSON.stringify({
'blue': window.glbBlue,
'eg2': 'eg3'
}),
contentType: "application/json; charset=utf-8",
success: function (result) {
alert("Success");
},
error: function (result) {
alert("A problem occured when submitting the form.");
}
});
e.preventDefault();
});
这将调用以下方法:
[HttpPost]
public ActionResult submitForm(string json)
{
System.Diagnostics.Debug.WriteLine("made it here");
var check = System.Web.Helpers.Json.Decode(json);
System.Diagnostics.Debug.WriteLine(check);
System.Diagnostics.Debug.WriteLine(check.glbBlue);
return View();
}
但是,控制器接收的JSON为空。 为什么会这样? 我在浏览器中看到有一个请求有效负载,具有我期望的值。 “ Window.glbBlue”是一个全局值,我也知道在使用警报检查其值时已正确设置了该值。
您发送数据
data: JSON.stringify({
'blue': window.glbBlue,
'eg2': 'eg3'
})
表示您的操作收到两个参数blue
和eg2
但您仅收到一个未提供的参数json
。 因此, json
为null
。
您可以将public ActionResult submitForm(string json) {}
更改为public ActionResult submitForm(string blue,string eg2) {}
。
要么
数据: JSON.stringify({json: "something" })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.