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