背景:ajax请求,发数组参数,后台springmvc
前端写法:
function toAuthorization(uuid){
var opt = {
title:'设置类别',
url:'${ctx}/developer/laozicloud/purchasecomputeorder/authorization?uuid='+uuid,
buttons: {
confirm: {
label: '确定',
className: 'btn btn-primary btn-w-m',
callback: function () {
var uuid = $("#uuid").val();
var computeCodes = [];
var computeCodeObj = $("input[name='computeCodes']");
if(computeCodeObj.length == 0){
$("#error").text("授权码不能为空");
$("input[name='computeCodes']")[0].focus();
return false;
}else{
$("#error").text("");
}
if(computeCodeObj.length >0){
var len = computeCodeObj.length;
for(var i=0;i<len;i++){
computeCodes[i] = computeCodeObj[i].value;
}
}
$.ajax({
url:'${ctx}/developer/laozicloud/purchasecomputeorder/saveAuthorization',
type:'post',
traditional: true,
data:{
'computeCodes':computeCodes,
'uuid':uuid
},
success:function(data){
if (data == '0'){//未登录
window.top.location.href = $ctx+$sysConstant.LOGIN_URL;
}else{
swal({
type: 'success',
text:'操作成功',
allowOutsideClick:false
}).then(function(){
$.vssware.doQuery('formId','#content',false,'#pageForm');
});
}
},
error:function(data){
alert(data);
swal({
type: 'error',
text:'操作失败',
allowOutsideClick:false
});
}
});
return true;
}
}
}
};
$.vssware.generateModal(opt);
}
这里前端是bootstrap 弹窗 保存发的ajax请求,获取参数都一样。主要是将参数发送到后台这里要注意下:
//定义一个数组,用来存放参数
var computeCodes = [];
//对参数进行赋值,直接采用下标即可
computeCodes【0】='test';
//发请求
$.ajax({
url:'${ctx}/developer/laozicloud/purchasecomputeorder/saveAuthorization',
type:'post',
traditional: true,//这个参数必须要加,反深度序列化的,加之前p=[a,b]加之后p=a&p=b这样后台才能拿到
data:{
'computeCodes':computeCodes,
'uuid':uuid
},
后台:
@ResponseBody
@RequestMapping("/saveAuthorization")
public String saveAuthorization(String uuid,String[] computeCodes,HttpServletRequest request) {
PurchaseComputeOrder order = getPurchaseComputeOrderService().find(uuid);
EnterpriseCompute compute = order.getCompute();
if(computeCodes != null && computeCodes.length>0) {
List<String> list= Arrays.asList(computeCodes); //将数组转化成list,这里的对象是Array&ArrayList
compute.setComputeCodes(new ArrayList<>(list));//这里是转化成Arraylist对象,这样保存后底层在调用list.remove方法才不会抛出unsupported exception
}
enterpriseComputeService.merge(compute);
order.setCompute(compute);
getPurchaseComputeOrderService().merge(order);
return "success";
}