如何通过Ajax访问发布的数据

时间:2019-04-12 23:48:37

标签: asp.net-core-2.1 razor-pages

我正在找到剃须刀页面,但卡住了一点。我有以下html

@Html.AntiForgeryToken()
<a href="#" onclick="deleteCustomer(5)">delete</a>

@section Scripts{
    <script type="text/javascript">

    function deleteCustomer(id) {
            $.ajax({
                type: "POST",
                url: "/Customers?handler=Delete",
                data: { id: id },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                // AntiforgeryToken is required by RazorPages
                headers: {
                    RequestVerificationToken: $('input:hidden[name="__RequestVerificationToken"]').val()
                }
            })
            .done(function () {
                alert("success");
            })
            .fail(function () { 
                alert("error");
            });
        }

    </script>
}

当我单击删除时,我进入 OnPostDelete 操作,但是 id 参数为

public JsonResult OnPostDelete(int id)
{
    return new JsonResult(new { success = true });
}

我如何访问通过ajax发送的 id 参数?


更新: 这样就可以了

ajax

data: JSON.stringify(id)

动作

public JsonResult OnPostDelete([FromBody]int id)

0 个答案:

没有答案