微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ajax 传递数组参数

 

一、ajax 传递数组参数

需要添加: Traditional: true,

                    let typeIDArr = [1,2,3,4,5,6];
                    var that = this;
                    var url = @Url.Action("GetDictionaryByTypeIDArray","Dictionary");
                    var data = { typeIDArray: typeIDArr };                  
                    $.ajax({
                        url: url,data: data,type: "get",dataType: "json",dynsc: false,Traditional: true,success: function (result) {
                                console.log(GetDictionaryByTypeIDArray,result)
                                if (result != null && result.length > 0) {
                                    that.dictionary = result.rows;
                                }
                        }
                    })
                    //f12 查看传递的参数为:
                    //typeIDArray: 1
                    //typeIDArray: 2
                    //typeIDArray: 3
                    //typeIDArray: 4
                    //typeIDArray: 5
                    //typeIDArray: 6

                    //失败案例二:
                    //data: JSON.stringify(data);
                    //f12 查看传递的参数为:
                    //{"typeIDArray":[1,2,3,4,5,6]}:

失败案例一: ajax 不添加Traditional: true,

                    //f12 查看传递的参数为:
                    //typeIDArray[]: 1
                    //typeIDArray[]: 2
                    //typeIDArray[]: 3
                    //typeIDArray[]: 4
                    //typeIDArray[]: 5
                    //typeIDArray[]: 6

二、后台为 C# MVC控制器方法,接收成功:

 

分享图片

 

 MVC 能将下面的键值对参数处理成数组
                    //typeIDArray: 1
                    //typeIDArray: 2
                    //typeIDArray: 3
                    //typeIDArray: 4
                    //typeIDArray: 5
                    //typeIDArray: 6

拓展:
https://www.cnblogs.com/hao-1234-1234/p/10308900.html

不同于MVC和ajax,在webApi axios 情况下尝试失败了,并没有将键值对参数处理成数组。
可能因为路由设置或过滤器设置导致失败,因为据说有人尝试成功了。
参考文档:
https://www.cnblogs.com/accessking/p/6664302.htmlhttps://www.cnblogs.com/zhaokunbokeyuan256/p/7477286.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐