SpringMVC接收Ajax请求几种常用方式

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方式,参数匹配方式接收
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值