在开发中经常遇见前端后端传参的问题,整理如下
以下都是ajax请求
1.Get 一般参数


2.POST一般参数可以和上面的GET请求一样只是改请求类型就可以,也可写成


3.接收数组需要使用POST和GET请求一样,只是改变请求类型


4.数组加其他类型混合参数


@RequestMapping(value="/test",method = RequestMethod.GET)
@ResponseBody
public String test(@RequestParam(value = "ar1[]")Integer[] ar1)
{ System.out.println(ar1); return "a";}
5.接收对象

ar1 = {"name":"tom","pwd":"123","id":"213231"};
$.ajax({
type:"POST",
url:"/test",
data:JSON.stringify(ar1),
dataType:"json",
contentType:"application/json",
success:function(data){
alert(data)
},
});

@RequestMapping(value="/test",method = RequestMethod.POST)
@ResponseBody
public String test(@RequestBody User user){
System.out.println(user);
return "a";
}
6.接收对象数组或者list



@RequestMapping(value="/test",method = RequestMethod.POST)
@ResponseBody
public String test(@RequestBody List user){
System.out.println(user);
return "a";
}
网友评论