[英]Ajax Post passing null to Controller
I am making an AJAX POST request to an MVC controller, yet the data is coming through as null
.我正在向 MVC 控制器发出 AJAX POST 请求,但数据作为null
。 I'm not sure why.我不知道为什么。
$(document).ready(function() {
$("#btnSaveCustomer").click(function() {
debugger;
var data = {
_DcLink: "1",
_Account: "Test",
_Name: "TestName",
_Title: "Mr",
_Init: "T"
}
$.ajax({
method: "POST",
url: '/Customer/SaveCustomer',
data: JSON.stringify(data),
success: function() {
debugger;
console.log(data)
alert("Success")
},
error: function() {
alert("Error")
}
});
})
public ActionResult SaveCustomer(string data)
{
using (var ms = new MemoryStream(Encoding.UTF32.GetBytes(data)))
{
// Deserialization from JSON
DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(Customer));
Customer serializeddata = (Customer)deserializer.ReadObject(ms);
}
return Json(new { Success = true });
}
No matter how I try to serialize the data it is always null
.无论我如何尝试序列化数据,它始终为null
。 I believe the AJAX POST method is not executing properly我相信 AJAX POST 方法没有正确执行
I am using dummy data in order to resolve the problem.我正在使用虚拟数据来解决问题。
The code hits a breakpoint at MemoryStream
in the controller - stating data is null
.代码在控制器中的MemoryStream
处遇到断点 - 声明数据为null
。
System.ArgumentNullException: 'String reference not set to an instance of a String. System.ArgumentNullException: '未将字符串引用设置为字符串的实例。 Parameter name: s'参数名称:s'
Any help appreciated任何帮助表示赞赏
It's because the ModelBinder is expecting a property in the JSON named data
, yet you're not sending that;这是因为 ModelBinder 期望 JSON 中有一个名为data
的属性,但您没有发送它; all the properties are in the root object.所有属性都在根对象中。 That would work fine if you were binding to a model, but as you're not you need to amend the data structure in your JS slightly, to this:如果您绑定到模型,那会正常工作,但由于您不是,您需要稍微修改 JS 中的数据结构,如下:
var data = {
data: {
_DcLink: "1",
_Account: "Test",
_Name: "TestName",
_Title: "Mr",
_Init: "T"
}
}
像这样改变;
data: {data : JSON.stringify(data)},
Having same issues.有同样的问题。 AJAX call fails. AJAX 调用失败。 If I remove [FromBody] from controller then the ajax call is successful however, the data is null.如果我从控制器中删除 [FromBody] 则 ajax 调用成功,但是数据为空。 I am using .NET Core 3.1 and razor pages我正在使用 .NET Core 3.1 和 razor 页面
<script>
$(document).ready(function () {
var data = { "name": "John Doe" }
$.ajax({
url: "/membership/tester/",
type: "POST",
data: JSON.stringify(data),
dataType: "json",
contentType: "application/json; charset=utf-8",
headers: {
RequestVerificationToken:
$('input:hidden[name="__RequestVerificationToken"]').val()
},
success: function (data) {
alert('Survey submitted successfully');
},
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 500) {
alert('Internal error: ' + jqXHR.responseText);
} else {
alert('Unexpected error.' + jqXHR.status);
}
}
});
});
</script>
Here is the controller code这是控制器代码
[Route("membership/tester")]
[HttpPost]
public ActionResult Test([FromBody] string somedata)
{
var data = somedata;
return Json(data);
}
$(document).ready(function() {
$("#btnSaveCustomer").click(function() {
debugger;
var data = {
_DcLink: "1",
_Account: "Test",
_Name: "TestName",
_Title: "Mr",
_Init: "T"
}
$.ajax({
method: "POST",
url: '/Customer/SaveCustomer',
data: JSON.stringify(data),
success: function(data) {
debugger;
console.log(data)
alert("Success")
},
error: function() {
alert("Error")
}
});
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.