简体   繁体   English

AJAX 在调用 ASP.NET MVC 控制器时收到 500 内部服务器错误

[英]AJAX receives 500 Internal server error when calling ASP.NET MVC Controller

I'am receiving a 500 internal server error when trying to call a controller action using Javascript AJAX.我在尝试使用 Javascript AJAX 调用控制器操作时收到 500 内部服务器错误。

I want to call a controller method when the user clicks the confirm button.我想在用户单击确认按钮时调用控制器方法。

CS Controller CS控制器

[HttpPost] 
[ValidateAntiForgeryToken]
public ActionResult Delete(string idFb, FormCollection from)
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(urlAPISignature);
        var deleteTask = client.DeleteAsync("api/whitepages/" + idFb);
        deleteTask.Wait();
        var result = deleteTask.Result;
        if (result.IsSuccessStatusCode)
        {
            return RedirectToAction("Index");
        }
        return RedirectToAction("Index");
    }
}

CSHTML File CSHTML 文件

    columns.Add().Encoded(false).Sanitized(false).Titled("").Filterable(false).RenderValueAs(model =>
           "<img title=\"supprimer\" class=\"delBtnWp\" data-idEpi=\"" + model.IdFb + "\" src=\"" + @Url.Content("~/Content/Images/trash-can.png") + "\" height=\"24\" width=\"24\" alt=\"del\" />");
.
.
.
.
<script>
        $(".delBtnWp").click(function () {
            var idFb = $(this).attr("data-IdFb");
            bootbox.dialog({
                title: 'Confirmer la suppression',
                message: 'Souhaitez-vous vraiment supprimer la ligne ?',
                buttons: {
                    danger: {
                        label: 'Non'
                    },
                    success: {
                        label: 'Oui',
                        className: "btn-success",
                        callback: function () {
                            success ();
                        }
                    }
                },
                callback: function (result) {
                    if (result) {
                        $.ajax({
                            type: 'POST',
                            url: "@Url.Action("Delete", "WhitePages")",
                            data: { IdFb: idFb },
                            success: function (data) {
                                alert("Yes");
                            }
                        });
                    }
                }
            });
        });
</script>

The compiler is not even reaching the controller action.编译器甚至没有达到控制器动作。

Regarding your case, Remove the [ValidateAntiForgeryToken] from your ActionMethod in your Controller, or generate a forgery token on your View and then send it in your AJAX call.关于你的情况,从你的控制器中的 ActionMethod 中删除[ValidateAntiForgeryToken] ,或者在你的视图上生成一个伪造令牌,然后在你的 AJAX 调用中发送它。 Also you need to change data-idEpi to data-idFb .您还需要将data-idEpi更改为data-idFb This would resolve your 500 Internal Server Error.这将解决您的 500 内部服务器错误。

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

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