前端传多个值给后端

前端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));

        }



    }
前端post后端数组可以通过以下步骤实现: 1. 首先,在前端页面中创建一个包含要后端的数组的表单。 2. 在表单中,使用多个input或select元素,将数组的每个元素作为一个表单项。 3. 在前端的JavaScript代码中,获取表单的数据,并将它们组织成一个数组。 4. 使用AJAX或fetch等技术,将数组数据通过POST请求发送给后端。 5. 在后端的服务器代码中,接收POST请求,并解析收到的请求体数据。 6. 将解析后的数据按需进行存储、处理或其他操作。 以下是一个简单的示例代码: 前端HTML代码: ``` <form id="postForm" action="backend.php" method="POST"> <input type="text" name="arrayElement[]" value="element1"> <input type="text" name="arrayElement[]" value="element2"> <input type="text" name="arrayElement[]" value="element3"> <button type="submit">提交</button> </form> ``` 前端JavaScript代码: ```javascript const form = document.getElementById('postForm'); form.addEventListener('submit', (e) => { e.preventDefault(); const formData = new FormData(form); const arrayData = Array.from(formData.getAll('arrayElement[]')); fetch(form.action, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(arrayData) }) .then(response => response.json()) .then(data => { console.log(data); // 后续操作 }) .catch(error => { console.error(error); }); }); ``` 后端PHP代码(backend.php): ```php $arrayData = json_decode(file_get_contents('php://input'), true); // 对数组数据进行处理或存储 ``` 这样,通过以上步骤,前端可以将数组数据通过POST请求后端进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值