前台怎么拿参数的我就不管了我也不会
反正用这个ajax没错
ajax 代码 一定要写明http请求类型 {
contentType:"application/x-www-form-urlencoded; charset=utf-8",
}这一段很重要(就是因为这个原因我找了整整一下午)
服务器为什么会对表单提交和文件上传做特殊处理,因为表单提交数据是名值对的方式,且Content-Type为application/x-www-form-urlencoded,
而文件上传服务器需要特殊处理,普通的post请求(Content-Type不是application/x-www-form-urlencoded)数据格式不固定,
不一定是名值对的方式,所以服务器无法知道具体的处理方式,所以只能通过获取原始数据流的方式来进行解析。
jquery在执行post请求时,会设置Content-Type为application/x-www-form-urlencoded,所以服务器能够正确解析,
而使用原生ajax请求时,如果不显示的设置Content-Type,那么默认是text/plain,这时服务器就不知道怎么解析数据了,
所以才只能通过获取原始数据流的方式来进行解析请求数据。
function orderFood(){ alert("进来了++++++++++"); final_settle(); /* alert("http://192.168.10.98: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的格式一定要写完整规范 不然不好取出来
有的问题实在解决不了的就重启电脑 哈哈哈