前端
$(function () {
$("#upload").click(function () {
const name = $(":input[name='name']").val();
const info = $(":input[name='info']").val();
var clazz = {};
clazz.name = name;
clazz.info = info;
$.ajax({
url: "/t/insertClass",
type: "post",
data: JSON.stringify(clazz),
contentType: "application/json",
success: function(flag){
if (flag) {
alert("添加成功");
} else {
alert("添加失败");
}
}
});
});
})
后台
@RequestMapping("/insertRemark")
@ResponseBody
public Boolean insertRemark(HttpServletRequest request,@RequestParam String content){
HttpSession session = request.getSession();
Student student = (Student)session.getAttribute("user");
student.settRemark(content);
Example example = new Example(Student.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("id", new Long(student.getId()));
example.and(criteria);
int flag = studentMapper.updateByExampleSelective(student,example);
return flag>0?true:false;
}
可能出现的错误
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
解决
ajax请求中没写contentType: "application/json"