[英]The required anti-forgery form field “__RequestVerificationToken” is not present in ajax call
[英]Invoking action from ajax method is throwing an error: The required anti-forgery form field “__RequestVerificationToken” is not present
我有以下ajax方法從控制器觸發動作方法。 執行以下方法時,我看到一個錯誤:所需的防偽表單字段“ __RequestVerificationToken”不存在。 但是在視圖源中,我看到有一個表單字段“ __RequestVerificationToken”,我正在將其傳遞給操作方法。
Test.cshtml:
<form action="javascript: void(0)" id="frmContent">
@Html.AntiForgeryToken()
</form>
<script type="text/javascript" charset="utf8">
var vue_data = vue_data || {};
(function () {
var token = $('#frmContent input[name=__RequestVerificationToken]').val();
$.ajax({
url: '@(Url.Action("GetData", "Test"))',
type: "POST",
cache: false,
async: false,
headers: {
"__RequestVerificationToken": token
},
data: { search: $('#search').val()},
success: function (data) {
var items = jQuery.parseJSON(data);
vue_data.view_content = items.items;
}
});
})();
</script>
TestController.cs
[ValidateHeaderAntiForgeryToken]
public JsonResult GetData(string search)
{
}
誰能幫我解決此問題?
您還可以在數據中傳遞防偽令牌。 我以前用過它,它就像一種魅力。
data: { "__RequestVerificationToken": token,"search": $('#search').val()}
在您的actionMethod上使用:[ValidateAntiForgeryToken]此外,這將不起作用。 數據:{search:$('#search')。val()}請在搜索中使用引號:數據:{“ __RequestVerificationToken”:令牌,“搜索”:$('#search')。val()}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.