1、 在Startup.cs文件中添加: services.AddMvc();
services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
2、在cshtml 文件中添加:
@Html.AntiForgeryToken()
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
3、前端代码
$('#btnPost').on('click', function () {
var item1 = $('#txtItem1').val();
var item2 = $('#txtItem2').val();
$.ajax({
type: "POST",
url: "RoleMenu?handler=Send",
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: JSON.stringify({
id: item1,
name: item2,
}),
contentType: "application/json; charset=utf-8&#