gpt4 book ai didi

javascript - 所需的防伪表单字段 __requestverificationtoken 不存在 ajax 调用时出错

转载 作者:行者123 更新时间:2023-12-01 01:44:20 27 4
gpt4 key购买 nike

anti-forgery form field “__RequestVerificationToken” is not present when using jQuery Ajax and the Html.AntiForgeryToken()
How to make ajax request with anti-forgery token in mvc
AJAX Posting ValidateAntiForgeryToken without Form to MVC Action Method

以上所有答案都没有帮助我。我在使用 Jquery Ajax 调用的请求中收到此错误:

"The required anti-forgery form field "__RequestVerificationToken" is not present"

如果我在 POST 操作方法中评论 [ValidateAntiForgeryToken] 属性,则它工作正常。我想知道为什么我会收到此错误。

@using (Html.BeginForm("Save", "AddPost", FormMethod.Post, new { id = "CreateForm" }))
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>GropPost_Table</h4>
<hr />
@Html.ValidationSummary(true)

<div class="form-group">
@Html.LabelFor(model => model.Body, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Body, new { id = "Bf" })
@Html.ValidationMessageFor(model => model.Body)
</div>
</div>


<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input id="btnAdd" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<小时/>
  [HttpPost]
[ValidateAntiForgeryToken]
public JsonResult Save([Bind(Include = "Body")] GropPost_Table groppost_table)
{

if (ModelState.IsValid)
{

groppost_table.GroupID = 1;
groppost_table.ID = 1;
groppost_table.PostDate = System.DateTime.Now;
db.GropPost_Table.Add(groppost_table);
db.SaveChanges();
return Json(groppost_table);
}

else
{

return Json("we Couldent add your post");
}
}
<小时/>
<script type="text/javascript">

$("#btnAdd").click(function () {

var GropPost_Table = {
"Body": $("#Bf").val()
};

var token = $('#CreateForm input[name=__RequestVerificationToken]').val()

var headers = {};

headers['__RequestVerificationToken'] = token;


$.ajax( {
type: "POST",
url: "@Url.Action("Save","AddPost")",
data: JSON.stringify(GropPost_Table),
contentType: "application/json;charset=utf-8",
processData: true,
headers:headers,
success: function (dataR) {
$("#Bf").val('');
},
error: function (dataR) {
$("#Bf").val('');
alert(dataR.toString());
}
});
});
</script>

最佳答案

我始终将请求验证 token 包含在 POST 数据中,而不是 header 中。我会这样处理:

首先将 type="submit" 添加到您的输入按钮,以便在单击时提交表单。然后在你的 JavaScript 中:

// Listen for the submit event on the form
$('#CreateForm').on('submit', function(event) {
var $form = $(this);

$.ajax({
// Html.BeginForm puts the url in the
// "action" attribute
url: $form.attr('action'),
// Serializing the form will pick up the verification
// token as well as other input data
data: $form.serialize(),
success: function(dataR) {
$('#Bf').val('');
},
error: function(dataR) {
$('#Bf').val('');
alert(dataR.toString());
}
});

// Preventing the default action will keep the form
// from doing a full POST.
event.preventDefault();
});

关于javascript - 所需的防伪表单字段 __requestverificationtoken 不存在 ajax 调用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30394473/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com