前台传数组:
var ids=[1,2,3,4,5,6]
$.ajax({ url:"http://localhost:8080/shanchu", type:"post", dateType:'json', data:{ ids:ids }, success:function(res){ var objs=eval(res); //解析json对象 //console.log("数据=="+JSON.stringify(objs));
console.log("数据=="+objs);
}, error:function(err){ alert("网络连接失败,稍后重试",err); } })
springboot接收传过来的数组:
@RequestMapping("/shanchu")
public String shanchubyid(@RequestParam(value = "ids[]") String[] ids){
System.out.println("参数:ids:"+JSON.toJSONString(ids));
return "成功";
}
前台传对象数组:
对象实体类(后台):
@Data
public class CaiPuEntity implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String weekDay;//星期几
@JSONField(format="yyyy年MM月dd日")
private Date startDate;//开始日期
@JSONField(format="yyyy年MM月dd日")
private Date endDate;//结束日期
private String foodOne;
private String foodTwo;
private String foodThird;
private String foodFour;
private String foodFive;
}
前台传的对象数组:
var food_day1={weekDay:"星期一",startDate:"2019年04月10日",endDate:"2019年04月10日",
foodOne:"黄焖鸡",
foodTwo:"黄焖鸡",
foodThird:"黄焖鸡",
foodFour:"黄焖鸡",
foodFive:"黄焖鸡",
};
var food_day2={weekDay:"星期一",startDate:"2019年04月10日",endDate:"2019年04月10日",
foodOne:"鸡公煲",
foodTwo:"鸡公煲",
foodThird:"鸡公煲",
foodFour:"鸡公煲",
foodFive:"鸡公煲",
};
var caipus=new Array();
caipus[0]=food_day1;
caipus[1]=food_day2;
传递:
$.ajax({
url:add_url,
data:JSON.stringify(caipus),
type:"post",
cache:false,
dataType: "json",
contentType:"application/json",
crossDomain: true == !(document.all),
success: function(res) {
console.log("新增==:"+JSON.stringify(res));
},
error:function(res) {
alert("联网失败,请检查网络");
}
});
后台接收:
//新增数据
@PostMapping("/add")
public String add(@RequestBody List caiPuEntityList){
log.info("新增="+JSON.toJSONString(caiPuEntityList));
return "新增成功";
}
参考:
https://www.jianshu.com/p/85251b746058
https://blog.csdn.net/qq_27093465/article/details/52094112