【问题标题】:Javascript, Ajax : Bad Request 400Javascript,Ajax:错误请求 400
【发布时间】: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


【解决方案1】:

我解决了这个问题,我只需要从我的控制器中删除 [ValidateAntiForgeryToken] 并直接在 Ajax 的正文中而不是 url 中传递数据。

【讨论】:

    【解决方案2】:

    当您使用 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 () {
                    }
                })
            });
    

    我还更改了 contentType 并添加了 dataType。

    更多关于ajax的信息发帖see documentation

    【讨论】:

    • 谢谢,我试过了,但总是相同的响应:加载资源失败:服务器响应状态为 400(错误请求):/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多