首页 技术 正文
技术 2022年11月15日
0 收藏 768 点赞 2,287 浏览 2380 个字

Ajax和SpringMVC之间的json数据传输有两种方式:

1.直接传输Json对象

2.将Json序列化成json字符串

1.直接传输Json对象

前端Ajax

$(document).ready(function(){
$("#btn_login").click(function(){
var dataJson = {
username:$("#username").val(),
password:$("#password").val()
};
$.ajax({
url:"/login/",
type:"post",
data:dataJson,
contentType:"application/x-www-form-urlencoded",//如不设置此项,默认也为此,设置发送给后端的类型
dataType:"json",//设置接收后端的数据的类型
async:true,//设置异步,不然可能接收不到后端返回的json
success:function(data){//data为后端返回的json
if(data.code==0){
window.location.reload();
}
else { }
}
});
});
});

后端使用

@RequestMapping(path = {"/login/"}, method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public String login(@RequestParam("username") String username,
@RequestParam("password") String password,
HttpServletResponse response) {
try {
Map<String, Object> map = userService.login(username, password);
if (map.containsKey("ticket")) {
Cookie cookie = new Cookie("ticket", map.get("ticket").toString());
cookie.setPath("/");
response.addCookie(cookie);
//return "redirect:/";
return CommonUtil.getJSONString(0, "成功");
} else {
//return "redirect:/";
return CommonUtil.getJSONString(1, map);
} } catch (Exception e) {
logger.error("登录异常" + e.getMessage());
//return "redirect:/";
return CommonUtil.getJSONString(1, "注册异常");
}
}

使用@RequestParam,即使用Servlet的request.getgetParameter。这种方式可以接受以application/x-www-form-urlencoded这种方式传输的JSON对象的。

2.将Json序列化

前端Ajax

$(document).ready(function(){
$("#btn_reg").click(function(){
var dataJson = {
username:$("#regusername").val(),
password:$("#regpassword").val()
};
$.ajax({
url:"/reg/",
type:"post",
contentType:"application/json",//以json字符串形式传输
data:JSON.stringify(dataJson),//将json对象序列化成字符串
dataType:"json",
async:true,
success:function(data){
if(data.code==0){
window.location.reload();
}
else { }
}
}); });
});

后端Controller

@RequestMapping(path = {"/reg/"}, method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public String reg(@RequestBody User user,
HttpServletResponse response) {
try {
Map<String, Object> map = userService.register(user.getUsername(), user.getPassword());
if (map.containsKey("ticket")) {
Cookie cookie = new Cookie("ticket", map.get("ticket").toString());
cookie.setPath("/"); response.addCookie(cookie);
return CommonUtil.getJSONString(0, "注册成功");
} else {
return CommonUtil.getJSONString(1, map);
} } catch (Exception e) {
logger.error("注册异常" + e.getMessage());
return CommonUtil.getJSONString(1, "注册异常");
}
}

@RequestBody中的user中,必须有与前端名称一致的属性,才可以接受到相应数据。

除此之外,@RequestBody还可用Map<String,Object> map来接收。

转载:https://my.oschina.net/u/3786691/blog/1823541

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,156
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,624
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,468
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,240
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,876
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,043