前端Ajax:
var ids= []; $.ajax({ url: '', data: { ids: ids}, dataType: "json", type: "POST", success: function (responseJSON) { alert('Ok'); } });
这样的写法后台用数组类型接收参数ids为null。只要在ajax请求时加上traditional:true就可以解决这个问题。
$.ajax({ url: '', data: { ids: ids}, dataType: "json", type: "POST", traditional: true,//这里设为true就可以了 success: function (responseJSON) { alert('Ok'); } });
一般我们传数参数组时这样写:
data:{arr:[1,2,3]}
如果单纯写成这样,在java后台是无法取到参数的,因为jQuery需要调用jQuery.param序列化参数:jQuery.param( obj, traditional )
默认的话,traditional为false,即jquery会深度序列化参数对象,但servelt api无法处理,我们可以通过设置traditional 为true阻止深度序列化,
然后序列化结果如下:
arr: ["123", "456", "789"] => arr=123&arr=456&arr=456
记录一下遇到的问题。