JQuery发送ajax请求不能用数组作为参数
JQuery发送ajax请求不能用数组作为参数,否则会接收不到参数,
一、js代码如下:
$('#delete-button').click(function(){
var selectedMembers = document.getElementsByName('selectedMembers');
var cwIds = new Array(); //定义数组
for(var i=0;i<selectedMembers.length;i++){
if(selectedMembers[i].checked)
cwIds.push(selectedMembers[i].value); //创建新数组并添加元素
}
var cwId = cwIds.join(","); //将新数组变成字符串类型
if(cwIds.length==0){
alert('请选择最少一条记录');
return false; //如果不成功则退出
}
$.post('${pageContext.request.contextPath}/cw/deleteCw',{cwId:cwId},function(msg){
if(msg){ alert("删除成功"); window.location.reload(); //ajax发送请求成功后即刻刷新页面并且把以前选中的取消选中
var elements = document.getElementsByName('selectedMembers');
if(elements){
for(var i = 0; i < elements.length; i++){
elements[i].checked = false;
}
}
}
else alert("删除失败");
});
});
后台接收的controller方法如下:
@RequestMapping(value="/deleteCw",method=RequestMethod.POST)
@ResponseBody
public String deleteCw(@RequestParam String cwId,HttpServletRequest request){
String[] cwIds = cwId.split(","); //将接收的字符串以","分割成字符串数组
int[] cw = new int[cwIds.length];
for(int i = 0;i<cwIds.length;i++){
cw[i] = Integer.parseInt(cwIds[i]); //字符串数组转换成整型数组
}
boolean flag =cwInfoService.deleteCw(cw);
if(flag) return "true";
return "";
}