前端表单:
用serialize()方法序列化表单之后用ajax传到后台,但总是传不过去,求教方法
{"name":"","birthday":"","IdNumber":"","tel":"","sex":"","address":"","cli_doc":"","reg_doc":"3"}这是你前端传的数据,如果想将其序列化为一个java对象,
你需要新建一个java(这里假设是User.class)类,其中的所有属性名和前端提交过来的一样,可以多出属性来,但不能少,然后你在后台就可以这样实现序列化
@ResponseBody
public Patient register(User user){
// 这里就可以使用user了
Patient returnPatient = new Patient();
return returnPatient;
}
把前台 ajax, 后台代码 拿过来看看
回复楼上:这就是我前台处理后得到的json数据:{"name":"","birthday":"","IdNumber":"","tel":"","sex":"","address":"","cli_doc":"","reg_doc":"3"}
这是ajax:$.ajax({
url:"/gh/patient/saveAndReg",
type:"get",
contentType :"application/json;charset=utf-8",
dataType:"json",
data:JSON.stringify(jsonPatient),
success:function(data){
console.log(data);
}
});
这是后台:@RequestMapping("saveAndReg")
@ResponseBody
public Patient register(@RequestBody Patient patient){
Patient returnPatient = new Patient();
try {
returnPatient = patientService.register(patient);
} catch (Exception e) {
e.printStackTrace();
}
return returnPatient;
}
后台报错:java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
前台报错:jquery.min.js:5 GET http://localhost:8080/gh/patient/saveAndReg?{%22name%22:%22%22,%22birthday%…22:%22%22,%22address%22:%22%22,%22cli_doc%22:%22%22,%22reg_doc%22:%223%22} 400 (Bad Request)
你好,对于对象接收前端数据的日期该怎么处理能够告诉一下我吗?谢谢
public AjaxResult doEditScjsRd(@RequestBody ScjsRd scjsRd) {
接收方法的时候加一个@RequestBody