前台怎么拿参数的我就不管了我也不会反正用这个ajax没错 ajax 代码 一定要写明http请求类型 {contentType:"application/x-www-form-urlencoded; charset=utf-8", ...
前台怎么拿参数的我就不管了我也不会
反正用这个ajax没错
ajax 代码 一定要写明http请求类型 {
contentType:"application/x-www-form-urlencoded; charset=utf-8",
}这一段很重要(就是因为这个原因我找了整整一下午)
function orderFood(){ alert("进来了++++++++++"); final_settle(); /* alert(":8080/jeesite/f/cms/received/submit"); */ console.log(JSON.stringify(globalJsonArray)); $.ajax({ type : "post", url : "/jeesite/f/received/submit", dataType:"json", contentType:"application/x-www-form-urlencoded; charset=utf-8", data:{orderInfo:jsonStr},//JSON.stringify(globalJsonArray), success : function(result) { alert(result.msg); } }); }
后面接收的代码 写好方法名称还有请求类型
@Controller@RequestMapping(value = "${frontPath}/received")public class FrontReceivedata extends BaseController{ @RequestMapping(value = "submit",method =RequestMethod.POST) public String submit(HttpServletRequest request,HttpServletResponse response){ String week = request.getParameter("orderInfo"); System.out.println("====================================="); System.out.println("requestJson:"+week); System.out.println("=====================================");
定几个全局变量 然后吧json转换成JSONObjcet格式(可以百度下格式)for循环下得到相应的值
最好加个判空的if
获取到了值放进那张表里保存的方法里面
for(int i =0 ;i<jsonArray.size(); i++){ //转换 //JSONObject list = jsonArray.getJSONObject(i); JSONObject list = (JSONObject) jsonArray.get(i); //各变量的if(){} //测试值 S System.out.println("orderList:"+orderList); System.out.println("allpriice:"+allpriice); System.out.println("num:"+num); //判断为空 if(list.get("orderlist")!= null){ orderList = (JSONArray)list.get("orderlist"); } if(list.get("allpriice") != null){ allpriice = (String)list.get("allpriice"); } if(list.get("num") != null){ num = (String)list.get("num"); } } //把数据放进保存方法里面 WOrder worder = new WOrder(); String a="WXDC"; //订单号前缀 Date dt= new Date(); Long time= dt.getTime();//这就是距离1970年1月1日0点0分0秒的毫秒数 String SingleNumber=a+time; //完整订单号 worder.setCid(SingleNumber); //订单号 worder.setAmount(allpriice);//总金额 worder.setOrderNumber(num);//桌号 Worderservice.save(worder);
如果json里面有多个字段 就用两个for循环 循环出来 然后保存进指定的数据库表里
if(orderList.size()!=0){ for(int j=0 ;j<orderList.size();j++){ JSONObject orderInfo = (JSONObject) orderList.get(j); //判断为空 if(orderInfo.get("number") != null){ number = (String)orderInfo.get("number"); } if(orderInfo.get("foodid") != null){ foodid = (String)orderInfo.get("foodid"); } System.out.println("number+++:"+number); System.out.println("fooddid+++:"+foodid); //数据保存方法 WChildorder wchildorder = new WChildorder(); wchildorder.setOid(SingleNumber); //订单号 wchildorder.setNumber(number);//分量 wchildorder.setDid(foodid);//菜的id wchiservice.save(wchildorder); } }
最好抛个异常 让前台判断 返回的是一个map 不过返回的map 方法上面需要加@ResponseBody这个注释
result.put("result", "保存成功"); result.put("code", "200"); result.put("SingleNumber", SingleNumber); } catch (Exception e) { e.printStackTrace(); result.put("result", "保存失败"); result.put("code", "100"); } //返回的 return result; }
到这就差不多了 自己写的过程中多测试测试 多写几个输出语句看有没有拿到值
还有的前台传过来json的格式一定要写完整规范 不然不好取出来
有的问题实在解决不了的就重启电脑 哈哈哈
原标题:ajax 发送json 后台接收 遍历保存进数据库
关键词:JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。