SpringMVC—对Ajax的处理(含 JSON 类型)(3)
五、服务器端的 SpringMVC 如何返回 JSON 类型的字符串。
请求:
$("#testJson8").click(function () {
$.ajax({
url: "testReturnJsonValue",
type: "post",
success: function (result) {
console.log(result);
}
});
});
1.返回单个对象
handler 方法:
@ResponseBody
@RequestMapping("/testReturnJsonValue")public Person testReturnJsonValue() {
Person person = new Person();
person.setName("lily");
person.setAge(23); return person;
}
在浏览器控制台正常打印了 Person 对象。
注意:这里没有指定 dataType。
2.返回多个对象
handler 方法:
@ResponseBody
@RequestMapping("/testReturnJsonValue")public List<Person> testReturnJsonValue() {
List<Person> personList = new ArrayList<>(); Person person = new Person();
person.setName("lily");
person.setAge(23);
Person person2 = new Person();
person2.setName("lucy");
person2.setAge(33);
personList.add(person);
personList.add(person2); return personList;
}
在浏览器控制条正常打印了 Person 数组。
3.返回 Map
@ResponseBody
@RequestMapping("/testReturnJsonValue")public Map<String, Person> testReturnJsonValue() {
Map<String, Person> map = new HashMap<>(); Person person = new Person();
person.setName("lily");
person.setAge(23);
Person person2 = new Person();
person2.setName("lucy");
person2.setAge(33); map.put("1", person);
map.put("2", person2); return map;
}
浏览器控制台输出:
4.在实际生产环境下的 Ajax 返回值。
封装一个返回值类型:
public class AjaxResult implements Serializable {
public static final String RESULT_CODE_0000 = "0000";
public static final String RESULT_CODE_0001 = "0001";
private String code;
private String message;
private Object data;
public AjaxResult() {
}
public String getCode() { return this.code;
}
public void setCode(String code) { this.code = code;
}
public String getMessage() { return this.message;
}
public void setMessage(String message) { this.message = message;
}
public Object getData() { return this.data;
}
public void setData(Object data) { this.data = data;
}
}
实际使用:
@ResponseBody
@RequestMapping("/testReturnJsonValue")
public AjaxResult testReturnJsonValue() {
AjaxResult ajaxResult = new AjaxResult();
try {
Map<String, Person> map = new HashMap<>(); Person person = new Person();
person.setName("lily");
person.setAge(23);
Person person2 = new Person();
person2.setName("lucy");
person2.setAge(33); map.put("1", person);
map.put("2", person2);
ajaxResult.setData(map);
ajaxResult.setMessage("success!");
ajaxResult.setCode(AjaxResult.RESULT_CODE_0000);
} catch(Exception e) {
e.printStackTrace();
ajaxResult.setMessage("fail!");
ajaxResult.setCode(AjaxResult.RESULT_CODE_0001);
} return ajaxResult;
}

六、Request Payload
(1)出现的条件:
contentType: 'application/json;charset=utf-8'
type:post
(2)具体请参看
http://xiaobaoqiu.github.io/blog/2014/09/04/form-data-vs-request-payload/
(3)建议尽量不要手动的去处理此种情况,能选用别的方式避免就尽量避免。
七、总结
主要介绍了SpringMVC 对 Ajax 的支持,对与 Ajax 数据如何组织,重点介绍了对表单的支持。
SpringMVC—对Ajax的处理(含 JSON 类型)(3)的更多相关文章
- SpringMVC—对Ajax的处理(含 JSON 类型)(2)
这里编写了一个通用的类型转换器: 用来转换形如: firstName=jack&lastName=lily&gender=1&foods=Steak&foods=Piz ...
- SpringMVC—对Ajax的处理(含 JSON 类型)(1)
一.首先要搞明白的一些事情. 1.从客户端来看,需要搞明白: (1)要发送什么样格式的 JSON 数据才能被服务器端的 SpringMVC 很便捷的处理,怎么才能让我们写更少的代码,如何做好 JSON ...
- 本文使用springMVC和ajax,实现将JSON对象返回到页面
一.引言 本文使用springMVC和ajax做的一个小小的demo,实现将JSON对象返回到页面,没有什么技术含量,纯粹是因为最近项目中引入了springMVC框架. 二.入门例子 ①. 建立工程, ...
- 通过Ajax进行POST提交JSON类型的数据到SpringMVC Controller的方法
现在在做的项目用到了SpringMVC框架,需要从前端angular接收请求的JSON数据,为了测试方便,所以直接先用AJAX进行测试,不过刚开始用平时用的ajax方法,提交请求会出现415或者400 ...
- springmvc通过ajax异步请求返回json格式数据
jsp 首先创建index.jsp页面 <script type="text/javascript"> $(function () { $("#usernam ...
- SpringMVC——对Ajax的处理(包含 JSON 类型)
一.首先要搞明白的一些事情. 1.从客户端来看,需要搞明白: (1)要发送什么样格式的 JSON 数据才能被服务器端的 SpringMVC 很便捷的处理,怎么才能让我们写更少的代码,如何做好 JSON ...
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
最近angularjs post到后台 400一头雾水 没有任何错误. 最后发现好文,感谢作者 SpringMVC中出现" 400 Bad Request "错误(用@Respon ...
- springMVC参数绑定JSON类型的数据
需求就是: 现在保存一个Student,并且保存Student的friend,一个student会有多个朋友,这里要传递到后台的参数是: var friends = new Array(); var ...
- springmvc接收JSON类型的数据
1.在使用AJAX传递JSON数据的时候要将contentType的类型设置为"application/json",否则的话会提示415错误 2.传递的data需要时JSON类型的 ...
随机推荐
- [BZOJ4730][清华集训2016][UOJ266] Alice和Bob又在玩游戏
题意:俩智障又在玩游戏.规则如下: 给定n个点,m条无向边(m<=n-1),保证无环,对于每一个联通块,编号最小的为它们的根(也就是形成了一片这样的森林),每次可以选择一个点,将其本身与其祖先全 ...
- 关于es集群转换为单点后,主分片丢失的问题(健康检测状态为red)
正在找解决方案 前后情况是, 之前是es双节点,之后更改为单节点,data中的数据都是双节点的,也许导致了单节点的状态不正常,删除了data目录下内容后,重启es,好了,这是测试环境,所以这么干的
- python之单元测试框架—unittest
一. 什么是单元测试?单元测试的对象是什么? 1: 什么是单元测试? 按照定义,单元测试就是对单个模块或者单个类或者单个函数进行测试,一般是开发做的,按照阶段分,一般就是单元测试.集成测试.系统测试. ...
- cocos2d-js 创建帧动画
封装创建方法 var RunActionHelper = function(){ };RunActionHelper.prototype.createAnimationByPlist = functi ...
- hzau 1200 Choosy in Food
1200: Choosy in Food Time Limit: 1 Sec Memory Limit: 1280 MBSubmit: 32 Solved: 5[Submit][Status][W ...
- uva 10739 dp
https://vjudge.net/problem/UVA-10739 和昨天的那个回文串几乎一样只是加了条件限制,可以随意增删以及替换. #include<iostream> #inc ...
- 51nod 1272 思维/线段树
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1272 1272 最大距离 题目来源: Codility 基准时间限制:1 ...
- Ceph pg分裂流程及可行性分析
转自:https://www.ustack.com/blog/ceph-pg-fenlie/ 1 pg分裂 Ceph作为一个scalable的分布式系统,集群规模会逐渐增大,为了保证数据分布的均匀性, ...
- nexus上传jar带依赖
编写pom文件 比如我上传alipay-sdk-java.jar 依赖是commons-logging.jar <project> <modelVersion>1.3.1& ...
- Asp.Net MVC session跨域
目的 在公司项目的某个特定场景中,需要在站点B的后端伪造请求,获取站点A的登录状态,抓取站点A的页面内容,因此要用实现session的跨域.以注册功能为例. 步骤 原理 简单地说,对于ASP.Net应 ...