jquery的ajax请求与springMVC交互
2014-04-22 18:58
302 查看
通常借助google的json包Gson将需要返回到前台的数据(对象、集合等)转为json格式的字符串后再返回,如:
Controller里的写法:
final Paginator notPassLeaves = this.leaveService.findLeaves(currentPage, 5,
leave);//自定义分页对象
Gson gson = new Gson();
String str = gson.toJson(notPassLeaves);
try {
response.setContentType("text/html; charset=utf-8");
response.setCharacterEncoding("utf-8");
response.getWriter().print(str );
} catch (final Exception e) {
WebHelper.logger.error("Error occurred.", e);
} finally {
try {
response.getWriter().close();
} catch (final IOException e) {
}
}
前台数据处理:
$.get(url,{currentPage:cPage},function(data){
var json = eval('('+data+')'); ///注意写法,常常因为没有用引号引而报缺少分号的错误
currentPage = json.currentPage;//json是返回的分页对象,通过点.的方式取属性
totalSize = json.totalSize;
pageSize = json.pageSize;
$.each(json.items,function(x,y){//items是分页对象里的数据list
x是索引,y是数据实体对象
});
});
Controller里的写法:
final Paginator notPassLeaves = this.leaveService.findLeaves(currentPage, 5,
leave);//自定义分页对象
Gson gson = new Gson();
String str = gson.toJson(notPassLeaves);
try {
response.setContentType("text/html; charset=utf-8");
response.setCharacterEncoding("utf-8");
response.getWriter().print(str );
} catch (final Exception e) {
WebHelper.logger.error("Error occurred.", e);
} finally {
try {
response.getWriter().close();
} catch (final IOException e) {
}
}
前台数据处理:
$.get(url,{currentPage:cPage},function(data){
var json = eval('('+data+')'); ///注意写法,常常因为没有用引号引而报缺少分号的错误
currentPage = json.currentPage;//json是返回的分页对象,通过点.的方式取属性
totalSize = json.totalSize;
pageSize = json.pageSize;
$.each(json.items,function(x,y){//items是分页对象里的数据list
x是索引,y是数据实体对象
});
});
相关文章推荐
- jQuery中的Ajax几种请求方法
- jquery.ajax 跨域请求webapi,设置headers
- 跨域请求之jQuery的ajax jsonp的使用解惑
- jquery 的ajax请求示例和注意事项
- dTree无限级目录树和JQuery同步Ajax请求
- 一段jQuery发送Ajax请求代码
- Ajax 校验 加jQuery 给Ajax发请求完整版
- jQuery中的Ajax几种请求方式
- JQuery的Ajax跨域请求的解决方案
- jQuery中的Ajax几种请求方法
- 【嘉兴东臣php】节点 ajax请求 jquery基本语法
- 跨域请求之jQuery的ajax jsonp的使用
- jQuery中的Ajax实现跨域请求,完成同源策略问题
- Ajax,Jquery请求WebService
- 使用Extjs,jquery,javascript进行ajax请求实例分析
- JQuery的ajax请求过程
- JQuery的 Ajax 跨域请求的解决方案
- 使用jQuery中的when实现多个AJAX请求对应单个回调的例子分享
- 夺命雷公狗jquery---53--jQuery里的ajax的底层实现POST请求
- 通过JQuery中的ajax请求数据