1、後臺代碼,一定要加上@RequestBody主要用來接收前端傳遞給後端的json字符串中的數據
@PostMapping("/test")
public String test(@RequestBody List<User> user) {
for (int i = 0; i < user.size(); i++) {
System.out.println(user.get(i));
}
return "OK";
}
2、前端代碼,ajax提交 。headers一定要設置
$.ajax({
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
url: prefix + '/test',
type: 'POST',
dataType: "json", //表示返回值類型,不必須
data: JSON.stringify(users),
success: function(res){
console.log(res);
}
});