简体   繁体   English

为什么我收到状态为 500 的错误(内部服务器错误)

[英]Why I get error with status of 500 (Internal Server Error)

I have one problem and unfortunately I cannot see error.我有一个问题,不幸的是我看不到错误。 The problem is following.问题如下。 I create Delete method and call that method in AJAX on button click.我创建 Delete 方法并在AJAX单击按钮时调用该方法。 But when I go to Index Page and try to delete item I get only error message which my swall aller dislay.但是当我 go 到索引页面并尝试删除项目时,我只收到我的 swall aller 显示的错误消息。

error: function (data) {
                        swal("Oops", "Something went wrong!", "error");
                    }

So far what I did, I create Delete Action in my controller到目前为止,我在 controller 中创建了Delete Action

[HttpPost,ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
    Language language = db.Languages.Find(id);
    db.Languages.Remove(language);
    db.SaveChanges();
    return RedirectToAction(nameof(Index));
}

And in my Index Page I create this:在我的Index Page中,我创建了这个:

<div class="panel panel-flat">
    <div class="panel-body">
        <table class="table datatable-responsive datatable-languages">
            <thead>
                <tr class="bg-blue">
                    <th>
                        Language Name
                    </th>
                    <th>
                        Country Code (Flag)
                    </th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr id="tr-id-@item.language_id">

                        <td>
                            @Html.DisplayFor(modelItem => item.name)
                        </td>

                        <td>
                            @Html.DisplayFor(modelItem => item.code)
                        </td>


                        <td>
                            <ul class="icons-list text-left">
                                <li class="text-primary-600"><a href="~/Languages/Edit?Id=@item.language_id"><i class="icon-pencil7"></i></a></li>
                                <li class="text-danger-600"><a class="delete_languages" href="javascript:;" data-id="@item.language_id"><i class="icon-trash"></i></a></li>
                            </ul>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>

And my AJAX call:我的 AJAX 电话:

<script>
    $('.datatable-languages').DataTable({
        dom: 'Bfrtip',
        columnDefs: [
            {
                responsivePriority: 1,
                targets: -1
            },
            {
                targets: [-1],
                orderable: false,
                searchable: false,
                printable: false,
                width: "120"
            }
        ]
    });

    $(document).on('click', '.delete_languages', function () {
        var myId = $(this).attr('data-id');
        console.log(myId);
        var parent = $(this).parent().parent().parent().parent();
        swal({
            title: "Are you sure?",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#FF7043",
            confirmButtonText: "Yes, delete it!",
            closeOnConfirm: false,
            showLoaderOnConfirm: true,
        },
            function () {
                $.ajax({
                    type: "post",
                    url: "/Languages/Delete",
                    data: { id: myId },
                    success: function () {
                        parent.fadeOut('slow', function () { $(this).remove(); $('.child').remove() });
                        setTimeout(function () { swal("Deleted!", "Record deleted successfully!", "success"); }, 2000);
                    },
                    error: function (data) {
                        swal("Oops", "Something went wrong!", "error");
                    }
                });

            })
    });
</script>

One notation before, I have try to remove AJAX call and in my Index Page I add this line of code, and It works之前的一个符号,我尝试删除 AJAX 调用,并在我的索引页面中添加了这行代码,它可以工作

<td>
    @Html.ActionLink("Edit", "Edit", new { id = item.BlogId }) <br />
    @Html.ActionLink("Details", "Details", new { id = item.BlogId }) <br />
    @Html.ActionLink("Delete", "Delete", new { id = item.BlogId })
</td>

Can anyone help me and tell me where I made mistake since I can not find, and have no idea where to start looking.谁能帮助我并告诉我哪里出错了,因为我找不到,也不知道从哪里开始寻找。 Everything looks good but Item can not be deleted and in console I get error message一切看起来都不错,但无法删除项目,在控制台中我收到错误消息

Internal Server Error (500)内部服务器错误 (500)

You have to use你必须使用

 return Json(new { success = true },JsonRequestBehavior.AllowGet);

After deleting ajax will go back to the success method but after deleting you are trying to redirecting it, So, it's giving you the error of 500. Ajax has the property that it does not reload your page and perform backend tasks and then you can reload that list that you want to update. After deleting ajax will go back to the success method but after deleting you are trying to redirecting it, So, it's giving you the error of 500. Ajax has the property that it does not reload your page and perform backend tasks and then you can reload您要更新的列表。

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

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