【发布时间】:2020-09-21 01:25:32
【问题描述】:
我正在处理 ASP Net Core 项目,我正在尝试使用 JQuery Ajax 函数从部分视图模式参数发送到我的控制器。
恢复的 URL 是正确的,例如:http://localhost:44321/Validations/ValidationRefuse?ficheId=24&commentaire=Commentaire%20de%20test
但它总是: 加载资源失败:服务器响应状态为 400(错误请求)
我的 Javascript :
$("#buttonRefusFiche").click(function (e) {
ficheId = $("#ficheId").val();
commentaire = $("#inputCommentaire").val();
$.ajax({
url: "/Validations/ValidationRefuse?ficheId=" + ficheId + "&commentaire" + commentaire,
type: 'POST',
contentType: 'application/html',
cache: true,
success: function () {
alert("Validation refusée.");
},
error: function () {
}
})
});
我的 C# 方法:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ValidationRefuse(int ficheId, string commentaire)
{ ... }
我的部分观点:
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Validez-vous cette fiche ?</h5>
<button type="button" class="btn btn-outline-dark btn-circle" data-dismiss="modal" aria-label="Close">
<i class="fa fa-close"></i>
</button>
</div>
<form asp-controller="Validations" asp-action="ValidationParResponsable" asp-route-id="@Model.FicheId" asp-route-eId="@Model.EnseignantId">
<div class="modal-body">
<div class="form-group">
<input type="hidden" id="ficheId" asp-for="FicheId" />
<input type="hidden" asp-for="EnseignantId" />
<input type="hidden" asp-for="EtatId" />
<label class="control-label font-weight-bold">Commentaire :</label>
<textarea class="form-control" id="inputCommentaire" asp-for="Commentaire" placeholder="Votre commentaire"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" id="buttonRefusFiche">Refuser</button>
<button type="submit" class="btn btn-success">Valider</button>
</div>
</form>
希望可以理解,感谢您的回复。 :)
【问题讨论】:
-
将请求数据放在body中而不是URl中,或者在action中的参数中添加
[FromQuery]属性 -
删除
contentType:- 你不是在发送 html。您可能会将其与响应类型dataType混淆,而contentType是您 发送 数据的方式(您不是因为它在 url 中没有数据) . -
好的,我会试试你的建议
-
根据您的建议,包括在正文中输入数据而不是 URL 和控制台返回 POST localhost:44321/Validations/ValidationRefuse400 (Bad Request)
-
我解决了这个问题,我只需要从我的控制器中删除 [ValidateAntiForgeryToken]
标签: javascript c# jquery ajax asp.net-mvc