Spring Mvc后台接受的数据带有List时,需要在在ajax中添加content-Type:"application/json",将data用JSON.stringify(data)进行转换,如下
$.ajax({
url: Criminal.ctxPath + "/mentals/save",
type: 'post',
data: JSON.stringify(this.mental),
contentType: "application/json",
dataType: 'json',
success: function (data) {
if (data.success) {
Criminal.loadRow("mentals/record/" + data.body.id);
} else {
toastr.error(data.message);
}
},
error: function (XMLHttpRequest) {
console.log("erros");
if (XMLHttpRequest.status === 404) {
toastr.error("找不到资源");
} else if (XMLHttpRequest.status === 500) {
toastr.error("后台错误");
} else {
toastr.error("error");
}
}
})
后台需要加上@RequestBody注解,如下
@PostMapping("save")
public ResponseEntity<Response> saveMental(@RequestBody Mental mental) {
Mental returnMental = mentalService.addMental(mental);
return ResponseEntity.ok(new Response(true,"提交成功",returnMental));
}
网友评论