ASP.NET MVC AJAX 请求中加入 antiforgerytoken 解决“所需的防伪表单字段“__RequestVerificationToken”不存在”问题

在ASP.NET mvc中如果在表中使用了@Html.AntiForgeryToken(),ajax post不会请求成功
解决方法是在ajax中加入__RequestVerificationToken:

function Like(id) {
// 获取form表单
 var form = $('form');
// 获取token
 var token = $('input[name="__RequestVerificationToken"]', form).val();
 $.ajax({
    url: '/Manage/Like',
    type: 'POST',
    //   在传入的data中加入__RequestVerificationToken: token
    data: { __RequestVerificationToken: token, profileID: id },
    error: function (xhr) { alert('Error: ' + xhr.statusText); },
    success: function (result) {},
    async: true,
    processData: false
 }); 
}

参考:
-- https://stackoverflow.com/questions/14473597/include-antiforgerytoken-in-ajax-post-asp-net-mvc

原文地址:https://www.cnblogs.com/AlexanderZhao/p/11454473.html