且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

编辑页面上的ASP.Net MVC 5 Ajax请求错误__RequestVerificationToken不存在

更新时间:2021-07-12 23:15:38

防伪标记是一种防止跨站请求伪造攻击。当使用默认的MVC方法时,它有2个部分:

Anti-forgery token is a method of preventing Cross-Site Request forgery attacks. When using default MVC methods it has 2 parts to it:


  • 视图中的防伪标记。它通常使用html helper Html.AntiForgeryToken 。这会导致在表单上创建隐藏的输入字段 __ RequestVerificationToken

  • 属性 ValidateAntiForgeryToken 适用于您的控制器或操作方法。

  • Anti-forgery token in your view. It usually is used by using html helper Html.AntiForgeryToken. This causes hidden input field __RequestVerificationToken to be created on a form
  • Attribute ValidateAntiForgeryToken applied to your controller or action methods.

在您的情况下,错误表示您有 ValidateAntiForgeryToken 关于你的方法,但表单方法没有提交第二部分 - 字段 __ RequestVerificationToken 。为了解决这个问题,你应该在post方法中添加第二个参数:

In your case error indicates that you have ValidateAntiForgeryToken on your method, but the form method did not submit the second part - field __RequestVerificationToken. In order to fix that you should add a second parameter to your post method:

var params = "&agency_id=" + id;
params += params + "&__RequestVerificationToken=" + $('input[name=__RequestVerificationToken]').val();
$.ajax({
    url: funcs,
    type: "POST",
    data: params,
})

您可以在此博客文章