【问题标题】:mvc-unable to call controller action having antiforgery tokenmvc-无法调用具有防伪令牌的控制器操作
【发布时间】:2015-07-30 14:26:55
【问题描述】:

如果我从控制器中删除防伪令牌,我将通过 ajax 调用将上传的图像发送到控制器,一切正常,但如果我使用它,调用不会被命中,并且出现 500 错误。

控制器

   [ValidateAntiForgeryToken]
    [AuthenticationRequired]
    [HttpPost]
    public ActionResult ChangeProfilePicture(HttpPostedFileBase imageData)
    {
    }

查看

  @Html.AntiForgeryToken()

        <div class="modal-body" id="tilesDescription">
            <div class="row">
                <div class="col-md-12">
                    <div class="text-center">
                        <div class="fileUpload btn btn-primary">
                            <span>Select a photo from your computer</span>
                            <input id="uploadBtn" type="file" class="upload" name="imageData" accept="image/*" />
                        </div>
                        <div class="text-center">
                            <img id="imgprvw" alt="uploaded image preview" class="imgPreview hide" />
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-rounded btn-sm btn-tiles" data-dismiss="modal">Cancel</button>
            <button type="submit" class="btn btn-rounded btn-sm btn-tiles disabled" id="btnProfilePic" onclick=" changeProfilePic() ">Set as profile picture</button>
        </div>

ajax 调用

   function changeProfilePic() {
    var data = new FormData();
    data.append("imageData", file);
    //var form = $('#__AjaxAntiForgeryForm');
    var token = $('[name=__RequestVerificationToken]').val();
    console.log($('[name=__RequestVerificationToken]').val());
    data[" __RequestVerificationToken"]= token;
    console.log(data[" __RequestVerificationToken"]);
    $.ajax({
        type: "POST",
        data: data,
        processData: false,
        contentType: false,
        url: '@Url.Action("ChangeProfilePicture", "Account")',
        success: function (resultdata) {
            HideModelWindow();
            $.ajax({
                type: "POST",
                url: '@Url.Action("SetProfilePicture", "Account")',
                success: function (resultdata) {
                    $("#profilePicDiv").empty();
                    $("#profilePicDiv").append(resultdata);
                    alert("Profile picture changed successfully");
                }

            });
        }

    });
}

【问题讨论】:

    标签: ajax asp.net-mvc antiforgerytoken


    【解决方案1】:

    您的 ajax 调用未正确传递令牌。应该是

    ....
    data.append('__RequestVerificationToken', token);
    
    $.ajax({
        type: "POST",
        data: data,
        processData: false,
        ....
    

    【讨论】:

    • 那么我的数据将如何发布,data.append("imageData", file);
    • 查看更新以将其添加到表单数据中。但是如果你有一个表单,你可以通过使用FormData 序列化表单来简化这一点,就像this answer
    • 谢谢,现在它的工作早些时候我也尝试过,但它不工作,但现在它工作了
    猜你喜欢
    • 2015-01-11
    • 2016-03-23
    • 2019-06-09
    • 2014-10-03
    • 2017-07-21
    • 2021-06-09
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多