【问题标题】:How to receive json data from html如何从html接收json数据
【发布时间】:2018-06-01 18:58:40
【问题描述】:

我想将一些 Json 数据从 html 发送到我的 c# 控制器。我需要它,因为我想从这些数据中生成 PDF 文件。我找不到任何解决这个问题的方法。这是我的帖子的样子:

function sendJSONData() {
    var stringToSend = generatePdf();
    try {
        $.ajax({
            type: "POST",
            cache: false,
            data: stringToSend,
            dataType: "json",
            success: getSuccess,
            error: getFail
        });
    } catch (e) {
        alert(e);
    };
    function getSuccess(data, textStatus, jqXHR) {
        alert(data.Response);
    };
    function getFail(jqXHR, textStatus, errorThrown) {
        alert(jqXHR.status);
    };

}

【问题讨论】:

标签: json asp.net-core-mvc


【解决方案1】:

我的一些项目中的代码 sn-p,用作方向:

<script type="text/javascript">
    $(function () {
        $("#button").click(function () {
            var json = @Html.Raw(Json.Encode(object));

            $.ajax({
                url: '@Url.Action("Action", "Controller")',
                type: 'POST',
                dataType: 'json',
                data: JSON.stringify(json),
                contentType: 'application/json; charset=utf-8',
                success: function (result) {
                    $('.cartbadge').html(result.data);

                    for (i = 0; i < 2; i++) {
                        $('.cartbadge')
                            .animate({ marginTop: '-=' + '10px' }, 100)
                            .animate({ marginTop: '+=' + '10px' }, 100);
                    }

                    setTimeout(function () {
                        $('.default-navbar').load('/Home/ReturnPartialNavbar');
                        $('#cartModalHolder').load('/Home/ReturnPartialCartModal');
                    }, 400);
                },
                error: function (result) {
                    alert(result);
                }
            });
        });
    });
</script>

您没有指定控制器的 url。您的脚本应该如何知道将数据发送到哪里? ;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 2019-04-22
    • 2017-02-19
    • 1970-01-01
    • 2018-04-25
    相关资源
    最近更新 更多