【问题标题】:How to pass json data via ajax?如何通过ajax传递json数据?
【发布时间】:2013-11-25 22:51:33
【问题描述】:

我正在开发一个 Visual Studio 2012 MVC 程序。

我使用 ajax 向控制器发送数据并希望控制器返回一个 html 正文。数据为json格式。数据是字符串名称和十进制的 TotFees。

发现控制器中public ActionResult ImmPay(string Name) 中的参数值始终为null。最后我尝试只传递名称,但控制器端的名称值仍然为空。

我的代码有什么问题,如何解决这个问题?谢谢。

View:
    function ImmPay()
    {
        var name = "ASP";
        var TotFees = 100.01;       

        //var dd = "{\'name\':\'" + name + "\', \'TotFees\':\'" + TotFees + "\'}"; 
        //var dd = "{\'name\':\'" + name + "\', \'TotFees\':\'" + TotFees + "m\'}";

        dd = "{\'b\':\'" + b + "\'}";
        dd = JSON.stringify(dd);
        $.ajax({
            url: '@Url.Action("ImmPay", "Consult")',
            type: 'GET',
            async: true,
            data: dd,
            contentType: 'application/json',
            context: document.body,
            success: function (response, textStatus, jqXHR) {
                $("#dialog-immpay").html(response);
                $("#dialog-immpay").dialog("open");
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(textStatus);
            },
            complete: function () {
                ;
            }
        });

    }

Controller:
        public ActionResult ImmPay(string Name)
        {
        do something here
        }

【问题讨论】:

    标签: javascript ajax json


    【解决方案1】:

    JSON.stringify 接受一个对象或数组并将其转换为 JSON,因此您可以将数据构建为对象并像这样将其字符串化

    dd = JSON.stringify({b: b});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-18
      • 2021-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多