使用 jquery 发布表单数据:
$.ajax('API/Validate/Customer?column=1&rowid=2&vmnr=3&isik=4',
{
data: JSON.stringify({
headerData: $("#_form").serializeArray()
}),
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST"
});
它由 ASP.NET MVC4 Web API 控制器接收验证:
public class ValidateController : ApiController
{
public class Body
{
public Dictionary<string, string> headerData { get; set; }
public Dictionary<string, string> rowData { get; set; }
}
public HttpResponseMessage Validate(
string id,
string column,
string rowid,
int? vmnr,
string isik,
[FromBody] Body body,
string dok = null,
string culture = null,
uint? company = null
)
{ ...
控制器中的 body.headerData 值为 null。
根据回答在如何在Web API控制器Post方法中接收动态数据 https://stackoverflow.com/questions/33924458/how-to-receive-dynamic-data-in-web-api-controller-post-method
body.headerData 必须具有表单键。
然而,它是空的。
如何在控制器中获取 headerData 作为键、值对?
Chrome 开发者工具显示正文中发布了正确的 json:
{"headerData":[{"name":"Kalktoode","value":"kllöklö"},
{"name":"Kaal","value":""}
]}
我尝试删除
public Dictionary<string, string> rowData { get; set; }
下课但问题仍然存在。