我有这个 jquery 函数,我想将防伪令牌传递给要验证的控制器。但是,无论我是通过标题还是通过正文,我当前的方法都会返回错误 400。我把这个空表格放在我的页面顶部@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "form" })){ @Html.AntiForgeryToken()}这是jquery函数。我在标题和正文中有 requestverificationtoken,但它不起作用。$("#eventGenerateButton").on("click", function (e) { e.preventDefault(); var event_form_data = { "__RequestVerificationToken": token, "StartDate": $("#eventStartDate").val(), "EndDate": $("#eventEndDate").val(), }; $.ajax({ url: "@Url.Action("GenerateEventLogsReport", @ViewContext.RouteData.Values["controller"].ToString())", method: "POST", headers: { "__RequestVerificationToken": token}, xhrFields: { responseType: 'blob' }, data: JSON.stringify(event_form_data), contentType: "application/json", success: function (result) { GenerateReport(result,"EventLogs"); }, error: function (error) { console.log(error); } }); return false;});这是控制器[HttpPost][ValidateAntiForgeryToken]public FileResult GenerateEventLogsReport([FromBody]GenericReportDateViewModel Input){}
3 回答

Qyouu
TA贡献1786条经验 获得超11个赞
从 中删除令牌event_form_data,然后尝试发送请求标头,例如
var event_form_data = {
"StartDate": $("#eventStartDate").val(),
"EndDate": $("#eventEndDate").val(),
};
var token = $('input[name="__RequestVerificationToken"]').val();
$.ajax({
url: "/Home/AjaxPost",
method: "POST",
headers: { "RequestVerificationToken": token},
xhrFields: {
responseType: 'blob'
},
data: JSON.stringify(event_form_data),
contentType: "application/json",
success: function (result) {
},
error: function (error) {
console.log(error);
}
});
添加回答
举报
0/150
提交
取消