【问题标题】:Array inside object for jQuery getJSON not populated未填充 jQuery getJSON 的对象内的数组
【发布时间】:2016-01-29 06:51:17
【问题描述】:

我在尝试使用 jQuery getJSON 函数时遇到了一个相当特殊的问题。

我尝试通过制作这样的对象来发送我的参数:

var args = {
    from: "",
    to: "",
    customerId: "52"
    articles: ['12312', '21521']
};

然后调用getJSON函数:

$.getJSON('/Statistics/TimeGraph', args, function (response) {
    //Fill graph.
});

这就是问题的开始。我在控制器上收到了请求,但没有填充articles(其他参数是)。

控制器动作:

public JsonResult TimeGraph(DateTime? from, DateTime? to, int? customerId, string[] articles)
{
    return Json("", JsonRequestBehavior.AllowGet);
}

不能在这样的对象中发送数组吗?还是我缺少什么?

【问题讨论】:

    标签: c# jquery json asp.net-mvc


    【解决方案1】:

    繁体参数需要设置为true,否则无法正常工作。

    $.getJSON('/Statistics/TimeGraph', $.param(args,true), function (response) {
        //Fill graph.
    });
    

    或者简单的ajax调用

     $.ajax({
            type: "GET",
            url: "/Statistics/TimeGraph",
            data: args,
            success: function(response){
                 //Fill graph.
            },
            dataType: "json",
            traditional: true
        });
    

    【讨论】:

    • 哇,谢谢,它有效。介意解释一下这是什么意思吗?
    • 这只是 jquery 序列化数据的新方式(不是传统方式),它不适用于 asp.net mvc 标准模型解析器。因此,如果我们发送数组,我们需要始终使用传统方式。
    猜你喜欢
    • 2020-10-25
    • 2018-04-09
    • 1970-01-01
    • 2013-03-10
    • 2017-12-20
    • 2022-10-01
    • 2014-04-12
    • 1970-01-01
    • 2020-02-23
    相关资源
    最近更新 更多