编辑页面上的ASP.Net MVC 5 Ajax请求错误__RequestVerificationToken不存在 [英] ASP.Net MVC 5 Ajax request on Edit page Error __RequestVerificationToken is not present
问题描述
我使用jQuery Ajax创建了一个级联下拉列表和ASP.Net MVC
我已经设置了我的ajax请求OnChange下拉列表
这里是我的代码:
I created a cascade dropdown using jQuery Ajax and ASP.Net MVC I have set my ajax request OnChange of dropdown here is my code:
@Html.DropDownList("Agency_Id", null, htmlAttributes: new { @class = "form-control", @onchange = "bring_projects(this.value,'bring_projects_by_agency','Project_Id')" })
项目DropDown:
The projects DropDown:
@Html.DropDownList("Project_Id", null, htmlAttributes: new { @class="form-control"})
这是我的脚本:
function bring_projects(id, funcs, divname) {
var ajax_image = "<img src='./Content/loading.GIF' >";
$('#' + divname).html(ajax_image);
var params = "&agency_id=" + id;
$.ajax({
url: funcs,
type: "POST",
data: params,
})
.done(function (r) {
$('#' + divname).html(r);
});
}
它给出以下错误:
Server Error in '/' Application.
The required anti-forgery form field "__RequestVerificationToken" is not present.
注意:
我没有提交表格我只是做了ajax请求OnChange of DropDown in我的编辑页面。
Note: I am not Submiting the form I Just do ajax request OnChange of DropDown in my Edit page.
推荐答案
防伪标记是一种防止跨站请求伪造攻击。当使用默认的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,
})
您可以在此博客文章
这篇关于编辑页面上的ASP.Net MVC 5 Ajax请求错误__RequestVerificationToken不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!