SpringMVC接收Ajax请求几种常用方式
几种常用的SpringMVC接收Ajax请求方式,
参数类型以及contentType类型的区分
1.@RequestParam
2.@RequestBody
3.参数与前端请求匹配
4.对象接收
1.Get请求
前端请求
get请求都可以使用以下两种方式进行请求:
$.ajax({
type:"get",
data:{"userId":"123"},
url:'/Test/testRequestParamGet',
dataType: 'json',
success: function (data) {
alert(data.obj);
}
})
$.ajax({
type:"get",
url:'/Test/testRequestParamGet?userId=123',
dataType: 'json',
success: function (data) {
alert(data.obj);
}
})
后端接收
1.RequestParam方式接收
@RequestMapping(value = "/testRequestParamGet", method = RequestMethod.GET)
@ResponseBody
public AjaxRes testRequestParamGet(@RequestParam(value = "userId") String userId) throws Exception {
AjaxRes ar = new AjaxRes();
ar.setSucceed(userId);
return ar;
}
2.参数匹配
@RequestMapping(value = "/testAutoGet", method = RequestMethod.GET)
@ResponseBody
public AjaxRes testAutoGet(String userId) throws Exception {
AjaxRes ar = new AjaxRes();
ar.setSucceed(userId);
return ar;
}
3.对象实体
@RequestMapping(value = "/testBodyGet", method = RequestMethod.GET)
@ResponseBody
public AjaxRes testBodyGet(User user) throws Exception {
AjaxRes ar = new AjaxRes();
ar.setSucceed(user.getUserId());
return ar;
}
2.Post请求
(1)RequestParam方式接收参数
前端请求
$.ajax({
type:"post",
url:'/Test/testRequestParamPost?userId=123',
dataType: 'json',
success: function (data) {
alert(data.obj);
}
})
$.ajax({
type:"post",
data:{"userId":"123"},
url:'/Test/testRequestParamPost',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
dataType: 'json',
success: function (data) {
alert(data.obj);
}
})
后端接收
@RequestMapping(value = "/testRequestParamPost", method = RequestMethod.POST)
@ResponseBody
public AjaxRes testRequestParamPost(@RequestParam(value = "userId") String userId) throws Exception {
AjaxRes ar = new AjaxRes();
ar.setSucceed(userId);
return ar;
}
(2)RequestBody方式接收参数
前端请求
$.ajax({
type:"post",
data:JSON.stringify({"userId":"123"}),
url:'/Test/testRequestBodyPost',
contentType: 'application/json; charset=UTF-8',
dataType: 'json',
success: function (data) {
alert(data.obj);
}
})
后端接收
@RequestMapping(value = "/testRequestBodyPost", method = RequestMethod.POST)
@ResponseBody
public AjaxRes testRequestBodyPost(@RequestBody User user) throws Exception {
AjaxRes ar = new AjaxRes();
ar.setSucceed(user.getUserId());
return ar;
}
(3)参数匹配
前端请求
$.ajax({
type:"post",
data:{"userId":"123"},
url:'/Test/testAutoPost',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
dataType: 'json',
success: function (data) {
alert(data.obj);
}
})
$.ajax({
type:"post",
url:'/Test/testAutoPost?userId=123',
dataType: 'json',
success: function (data) {
alert(data.obj);
}
})
后端接收
@RequestMapping(value = "/testAutoPost", method = RequestMethod.POST)
@ResponseBody
public AjaxRes testAutoPost(String userId) throws Exception {
AjaxRes ar = new AjaxRes();
ar.setSucceed(userId);
return ar;
}
(4)对象接收
前端请求
$.ajax({
type:"post",
data:{"userId":"123"},
url:'/Test/testBodyPost',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
dataType: 'json',
success: function (data) {
alert(data.obj);
}
})
后端接收
@RequestMapping(value = "/testBodyPost", method = RequestMethod.POST)
@ResponseBody
public AjaxRes testBodyPost(User user) throws Exception {
AjaxRes ar = new AjaxRes();
ar.setSucceed(user.getUserId());
return ar;
}
3.总结
1.get请求:
前端可以使用URL拼接参数形式或者表单形式提交
后端可以使用@RequestParam,参数匹配接收,对象接收
get请求不可以使用@RequestBody接收
2.post请求
前端采用表单方式(application/x-www-form-urlencoded)提交时,后端可以使用@RequestParam方式,参数匹配方式,对象方式接收
前端采用application/json形式提交,后端使用@RequestBody方式接收
前端采用URL拼接参数形式提交,后端使用@RequestParam方式,参数匹配方式接收