derrickie
derrickie
2017-04-27 08:36
采纳率: 33.3%
浏览 5.9k
已采纳

springMVC中如何用对象来接收前端序列化的表单?

前端表单:

&nbsp&nbsp姓名:
出生日期:
身份证号:
联系电话:
&nbsp&nbsp性别: 请选择......男女
家庭住址:
门诊医生:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 添加并挂号
 用serialize()方法序列化表单之后用ajax传到后台,但总是传不过去,求教方法
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • qbian
    Qbian 2017-04-27 10:31
    已采纳

    {"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;
    }

    点赞 评论
  • weixin_37931485
    weixin_37931485 2017-04-27 09:16

    把前台 ajax, 后台代码 拿过来看看

    点赞 评论
  • derrickie
    derrickie 2017-04-27 09:36

    回复楼上:这就是我前台处理后得到的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)
    
    点赞 评论
  • derrickie
    derrickie 2017-04-27 14:09

    图片说明

    点赞 评论
  • qq897793423
    远方的兔子 2017-11-20 01:12

    你好,对于对象接收前端数据的日期该怎么处理能够告诉一下我吗?谢谢

    点赞 评论
  • LHD_JAVA
    衰帝 2019-07-30 17:16

    public AjaxResult doEditScjsRd(@RequestBody ScjsRd scjsRd) {
    接收方法的时候加一个@RequestBody

    点赞 评论

相关推荐