前端传多个值给后端

前端ajax请求
type:“post”,
data:JSON.stringify(op),

 $(".submitOrder").click(function () {

      //订单oid
      var oid=new Array();

      var oidList=$(".list-group-item");

      oidList.each(function () {
          oid.push(parseInt($(this).attr("oid")));
      });
      // console.log(oid);

      //订单count
      var count=new Array();
      var countList=$(".small");

      countList.each(function () {
          count.push(parseFloat($(this).val()));
      });
      // console.log(count);

      //订单小计
      var subtotal=new Array();
      var subtotalList=$(".subtotal");
      subtotalList.each(function () {

          subtotal.push(parseFloat($(this).html()));
      });
      // console.log(subtotal);

      var op=[
          {oid:oid},
          {subtotal:subtotal},
          {count:count}
      ];
      $.ajax({
          url:"/modifyCarCountSubtotal",
          type:"post",
          data:JSON.stringify(op),
          success:function (re) {
              console.log(re);
          }
      });


  });

后端接收数据:
BufferedReader reader = req.getReader();

@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
        PrintWriter out= resp.getWriter();

        req.setCharacterEncoding("utf8");
        BufferedReader reader = req.getReader();

        String readerStr="";
        String line;

        while ((line=reader.readLine())!=null){
            readerStr=readerStr.concat(line);
        }
//        System.out.println(readerStr);


        List list = new Gson().fromJson(readerStr, List.class);
//        System.out.println(list);

        for (Object o : list) {
            //遍历得到一个map对象
//            System.out.println(o instanceof Map);

        }

        //oid
//        System.out.println(list.get(0));
        //subtotal
//        System.out.println(list.get(1));
        //count
//        System.out.println(list.get(2));

        Map moid = (Map) list.get(0);
        Map msubtotal= (Map) list.get(1);
        Map mcount= (Map) list.get(2);
        List oidList = (List) moid.get("oid");
        List subtotalList = (List) msubtotal.get("subtotal");
        List countList = (List) mcount.get("count");


        boolean b=false;
        CarService carService = new CarService();
        for (int i = 0; i < oidList.size(); i++) {
            Double oid=(Double) oidList.get(i);
            System.out.println(oid.intValue());
            for (int i1 = 0; i1 < subtotalList.size(); i1++) {
                System.out.println(subtotalList.get(i1));
                Double subtotal = (Double) subtotalList.get(i);
                 b = carService.modifySubtotal(subtotal.floatValue(), oid.intValue());
            }
            for (int i1 = 0; i1 < countList.size(); i1++) {
                Double count = (Double) countList.get(i1);
                System.out.println(count.intValue());
                b = carService.modifyCount(count.intValue(), oid.intValue());

            }
        }

        if (b){
            AjaxResult as = new AjaxResult();
            as.setMsg("订单成功");
            as.setCode(1000);
            out.write(new Gson().toJson(as));

        }else {
            AjaxResult as = new AjaxResult();
            as.setMsg("订单失败");
            as.setCode(1001);
            out.write(new Gson().toJson(as));

        }



    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值