遇到的HTTP415/400问题错误解决方案:
客户端:
$(function(){
$("#send").click(function(){
varjsondata=$("#jsondata").val();
varjsonobj=JSON.parse(jsondata)
varcallback=function(data){
$("#result").html(JSON.stringify(data))
}
$.postJSON('<%=basePath%>api/user/test',jsonobj,callback)
});
})
$.postJSON=function(url,data,callback){
returnjQuery.ajax({
'type':'POST',
'url':url,
'contentType':'application/json',
'data':JSON.stringify(data),
'dataType':'json',
'success':callback
});
};
JSON对象
{"id":2,"gender":0,"mobile":"15072370640","password":"123456","accessToken":"fd6bf3dd3cca4b0ca7c9099447994dba"}
springmvc服务端:
@RequestMapping(value="/test",method=RequestMethod.POST)
public@ResponseBodyMap
@RequestBodyUserFormuserForm,
BindingResultbindingResult){
Map
if(bindingResult.hasErrors()){
map.put("errorCode","40001");
map.put("errorMsg",bindingResult.getFieldError().getDefaultMessage());
}
map.put("user",userForm);
returnmap;
}
测试:
结果
{"user":{"name":null,"id":2,"password":"123456","avatar":null,"gender":0,"mobile":"15072370640","accessToken":"fd6bf3dd3cca4b0ca7c9099447994dba"}}