前端使用Layui以ajax的方式提交json数据到后台,后台使用Springboot3,controller中以注解的方式约定了提交方式为post,默认数据为:application/json。
method = RequestMethod.POST,consumes = "application/json"
提交时出现如下错误:
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error:
原因是:在html中撰写的代码,data:……一定要使用JSON.stringify(data.field):
$.ajax({ url:"/studentSave", method:"POST", dataType:"json", data:JSON.stringify(data.field), contentType:"application/json;charset=UTF-8", success:function(){ layer.msg("success!"); } });