只接收一个List作为入参的话不报错,下方为代码:前台:$.ajax({ type:"post", url: 'add', contentType:"application/json; charset=utf-8", dataType:"json", data:JSON.stringify([{id:1,name:"hello"},{id:2,name:"hello"}]), success:function(data){ console.log(data); }})后台:@ResponseBody@RequestMapping(value = "add", consumes = "application/json; charset=utf-8")public String add(@RequestBody List<Tag> param) { System.out.println("param:" + param); return "成功";}前台控制台显示的数据格式为:[{id: 1, name: "hello"}, {id: 2, name: "hello"}]0:{id: 1, name: "hello"}1:{id: 2, name: "hello"}那如果我后台想接受一个字符串和一个list该怎么写呢,后台代码改为:@ResponseBody@RequestMapping(value = "add", consumes = "application/json; charset=utf-8")public String add(@RequestBody String content, @RequestBody List<Tag> param) {前台应该怎么写?
1 回答
杨__羊羊
TA贡献1943条经验 获得超7个赞
两种解决方案
1、
如果参数比较少 直接url?parm=value,
那么就用@RequestParam注解
2、封装成对象
class A {
private String parm1;
private String parm2;
private List<B> list;
}
对应的前台
var data =new Object;
data.param1="value";
data.param2="value";
var list = new Array();
....省略list的数据初始化
data.list = list;
ajax的data
data:JSON.stringify(data)
添加回答
举报
0/150
提交
取消