post请求中参数是以 form data 和 Request Payload形式提交的
form data
function submit() {
var postData = {
name:"txzhang",
age: 25,
weight:80
};
$.ajax({
url: "/hello/save",
type: "post",
dataType: "json",
data:postData,
success: function (data) {
alert("提交成功!");
}
});
}
正常情况下Content-Type是application/x-www-form-urlencoded;charset=UTF-8 ,这种情况后台就直接vo对象接受就可以。
@RequestMapping("/hello/save")
@ResponseBody
public R save(HelloVo vo) {
log.info(vo.toString());
return R.success("");
}
Request Payload
$.ajax({
url: "/hello/save",
type: "post",
contentType:"application/json;charset=UTF-8",
dataType: "json",
data: JSON.stringify(postData),
success: function (data) {
alert("提交成功!");
}
});
对应的后台代码, 需要用@RequestBody来修饰
@RequestMapping("/hello/save")
@ResponseBody
public R save(@RequestBody HelloVo vo) {
log.info(vo.toString());
return R.success("");
}