简体   繁体   English

Javascript、Ajax:错误请求 400

[英]Javascript, Ajax : Bad Request 400

I'm working on ASP Net Core Project and I'm trying to send to my controller with JQuery Ajax function from a partial view modal, parameters. I'm working on ASP Net Core Project and I'm trying to send to my controller with JQuery Ajax function from a partial view modal, parameters.

The recovered URL is correct for example: http://localhost:44321/Validations/ValidationRefuse?ficheId=24&commentaire=Commentaire%20de%20test恢复的 URL 是正确的例如: http://localhost:44321/Validations/ValidationRefuse?ficheId=24&commentaire=Commentaire%20de%20test

But it's always: Failed to load resource: the server responded with a status of 400 (Bad Request)但它总是:加载资源失败:服务器响应状态为 400(错误请求)

My Javascript:我的 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 () {
            }
        })
    });

My C# method:我的 C# 方法:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> ValidationRefuse(int ficheId, string commentaire)
        { ... }

My Partial View:我的部分观点:


<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>

I hope it's understandable, thanks for your responses.我希望它是可以理解的,谢谢你的回复。 :) :)

When you use a POST request you have to send the parameters in the body of the request, not in the URL like that:当您使用 POST 请求时,您必须在请求正文中发送参数,而不是像这样在 URL 中发送参数:

  $("#buttonRefusFiche").click(function (e) {
            ficheId = $("#ficheId").val();
            commentaire = $("#inputCommentaire").val();
            $.ajax({
                url: "/Validations/ValidationRefuse",
                type: 'POST',
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                dataType : "html",
                data : {
                    ficheId : ficheId,
                    commentaire : commentaire
                },
                cache: true,
                success: function () {
                    alert("Validation refusée.");
                },
                error: function () {
                }
            })
        });

I also changed the contentType and added dataType.我还更改了 contentType 并添加了 dataType。

For more information about ajax Post see documentation有关 ajax 发布的更多信息,请参阅文档

I resolved this problem, I just needed remove [ValidateAntiForgeryToken] from my controller and to pass data directly in the body of the Ajax instead the url.我解决了这个问题,我只需要从我的 controller 中删除[ValidateAntiForgeryToken]并直接在 Ajax 的正文中传递数据,而不是 url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM