ajax请求发数组参数,后台接收

背景: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";
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值