Spring MVC前后台传递JSON
来源:互联网 发布:htcg5软件 编辑:程序博客网 时间:2024/05/01 02:15
1. 传递JSON参数
vardata = {'id':1,'name':'abc'};
$.ajax({
type:'post',
url:'homePageAction.do?testAJax',
contentType:'application/x-www-form-urlencoded',
data:JSON.stringify(data),
success:function(data){
console.log("aaaaaaaaaaa")
console.log(data.msg);
},
error:function(){
}
});
// @RequestMapping(params= "testAJax")
// public voidtestAjax(@RequestParam String id,String name,HttpServletRequest req){
// Stringid2 = req.getParameter("id");
// Stringname2 = req.getParameter("name");
// System.out.println("1111");
// System.out.println("2222");
// }
2. 传递JSON对象或JSON数组(后台接收使用EventInfo[],而不是List<EventInfo> list)
vardata = [{'id':1,'name':'abc'},{'id':2,'name':'def'},{'id':3,'name':'ghi'}];
console.log(JSON.stringify(data));
$.ajax({
type:'post',
url:'homePageAction.do?testAJax',
contentType:'application/json',
data:JSON.stringify(data),
success:function(data){
console.log(data.msg);
console.log(data.obj.id);
console.log(data.obj.name);
},
error:function(){
}
});
// @RequestMapping(params= "testAJax")
// @ResponseBody
// publicJSONObject testAjax(@RequestBody EventInfo[] ei,HttpServletRequest req){
// Longid1 = ei[0].getId();
// Stringname1 = ei[0].getName();
// JSONObjectjo = new JSONObject();
// jo.put("msg","return success");
// jo.put("obj",ei[0]);
// return jo;
// }
3. 后台返回前台JSON,需要在返回方法上加上@ResponseBoby,代表将JSON数据放到Http Boby中返回
返回值标识了@ResponseBody,SpringMVC将使用StringHttpMessageConverter的write()方法,将结果作为String值写入响应报文,当然,此时canWrite()方法返回true。
关于HttpMessageConverter和@RequestBody、@ResponseBody的关系请看我另一篇文章。
- Spring MVC前后台传递JSON
- spring mvc 前后台json交互问题
- json的前后台传递
- Spring MVC通过JSON实现前后台数据交互
- Spring MVC 前后台数据交互
- Spring MVC 前后台数据交互
- Spring mvc Ajax前后台交互
- spring MVC 前后台 映射关系
- MVC特性---轻松实现前后台数据的传递
- 页面之间传递值,mvc参数问题(前后台)
- 前后台传递参数
- mvc使用ajax(json的前后台处理)
- 关于json数据的前后台交互(mvc)
- json前后台交互
- json前后台使用
- Spring MVC关于ztree插件进行前后台交互
- 关于Spring MVC前后台框架的设计
- Spring mvc +miniUI 前后台交互 报415错误
- PhoneView
- 网络协议逆向分析
- dpdk内存管理之内存分配器(堆分配)
- jdbc连接数据库,使用通配符
- middle-box
- Spring MVC前后台传递JSON
- mdadm代码解析
- JavaScript学习: NaN数据类型
- 小马哥和数列
- Java基础知识_day03_(选择流程控制语句,循环流程控制语句,控制循环语句)
- spring ioc 源码详解01
- 剑指Offer 7 用两个栈实现队列
- leetcode128. Longest Consecutive Sequence
- HDOJ 1213 How Many Tables 1232 畅通工程(并查集基础,水)