我正在使用jQuery DataTales从MVC5请求POST URL,并尝试添加防伪标记。我已将其添加到标题和请求正文中,但仍然收到500错误:“所需的防伪表单字段“__RequestVerificationToken”不存在。”
表单:
<form id="my-units-form" action="@Url.Action("MyUnitsResults", "Provider")" class="form-horizontal criteria well well-sm">
@Html.AntiForgeryToken()
....
JavaScript:
$userDt = $('#users-table')
.DataTable({
serverSide: true,
ordering: false,
searching: true,
ajax: {
"url": url,
"type": "POST",
'contentType': 'application/json',
"dataType": "json",
headers: { '__RequestVerificationToken': $('form input[name=__RequestVerificationToken]').val() },
data: function (d) {
d.__RequestVerificationToken= $('form input[name=__RequestVerificationToken]').val();
return JSON.stringify(d);
}
},