Spring MVC 下以JSON格式向后抬传数据

来源:互联网 发布:为什么mac不能玩lol 编辑:程序博客网 时间:2024/06/11 11:23

json 格式:

{type:1, appKey: "aaa", sign: "bbb"}

后台代码:

<span style="white-space:pre"></span>@RequestMapping(value="/getEventList", method=RequestMethod.POST)@ResponseBodypublic Object getEventList(@RequestBody Map<String, Object> map){logger.info("test");logger.info(map.get("type"));Map<String, Object> json = new HashMap<String, Object>();json.put("code", "200");return json;}


前台代码:

function dojson(){$.ajax({type: "post",dataType:"json",contentType: "application/json;charset=utf-8",        url: "<%=basePath%>getEventList",data: JSON.stringify({type:1, appKey: "aaa", sign: "bbb"}),        success: function (result) {        }});}

json 格式:

{type:1, productList:[{productId:1,stock:1},{productId:2,stock:2}]}

后台代码:

<span></span>@RequestMapping(value="/getEventList", method=RequestMethod.POST)@ResponseBodypublic Object getEventList(@RequestBody Map<String, Object> map){logger.info("test");logger.info(map.get("type"));List<LinkedHashMap<String, Object>> lista = (List<LinkedHashMap<String, Object>>) map.get("productList");System.out.println(lista.get(0).get("productId"));System.out.println(lista.get(0).get("stock"));Map<String, Object> json = new HashMap<String, Object>();json.put("code", "200");return json;}


前台代码:

function dojson(){$.ajax({type: "post",dataType:"json",contentType: "application/json;charset=utf-8",        <span style="white-space:pre"></span>url: "<%=basePath%>getEventList",data: JSON.stringify({type:1, productList:[{productId:1,stock:1},{productId:2,stock:2}]}),        success: function (result) {        }});}



















0 0
原创粉丝点击