0

下面是我尝试使用 JQuery Ajax 调用向 Spring MVC 控制器发布 Json 序列化对象(具有其他 pojo 列表的 java pojo)的代码。

收到来自服务器的错误请求错误。

当从 Json 对象中删除 modalForm 数据时,MainPojo 数据在服务器端正确接收。

html代码

   Fisrt form
   ------------- 
    <form id="mainForm">
           .....
    </form>

    Form in the modal
    -----------------
    <form  id="modelform" ..>
         <div id="row"> 
    <input type="text" id="subject" />
    <input type="text" id="max" />
    <input type="text" id="min" />
      </div>
     <div id="row"> 
    <input type="text" id="subject" />
    <input type="text" id="max" />
    <input type="text" id="min" />
     </div>
         ............. 
    </form>}

jQuery

          var mainObject = $('#mainForm').serializeObject();

          var modelObject = $('#modelform').serializeObject();

          mainObject.marks = modelObject;

Json(预期)

          {
            "name": "Some Name",
             "age": "10",
             "marks": [
               {
                 "subject": "maths",
                 "max": "20",
             "min":"12"
                },
               {
                 "subject": "english",
                 "max": "20",
             "min":"12",
                }
              ]
          }

Json(上面代码的实际输出)

           {
             "name": "Some Name",
              "age": "10",
              "marks": [
                        {
                         "subject": "maths",
                         "subject": "english"
                         },
                         {
                          "max": "20",
                       "max":"20",
                          },
                      {
                       "min": "12",
                       "min":"12"
                       }
                          ]
               }

                 //Ajax call
                 $.ajax({
                       url: '/save',
                   type: 'POST',
                   contentType: 'application/json',
                   mimeType: 'application/json',
                   data : JSON.stringify(mainObject),
                   dataType: 'json',
                   success: function(data) {
                      alert(data.msg);
                   },
                   error:function (xhr, ajaxOptions, thrownError) {
                      alert('Technical error occured');
                   }
           });

Java Pojo 的

          public class MainPojo {

            private String name;
            private String age;            
                private Lists<marks>
                ..................
          }

          public class ModelPojo {

            private String subject;
            private String maxMarks;
            private String minMarks;

                .....................
            }

控制器方法

@RequestMapping(value = "save", headers = "Accept=application/json",
                method = RequestMethod.POST)
public @ResponseBody String save(@RequestBody final  MainPojo  mainPojo) {

} 

请帮我找出问题所在。

谢谢你。

4

2 回答 2

2

像这样修改html文本

     <form id="mainForm">
    <input name="name" type="text" value="Some Name">
    <input name="age" type="text" value="20">
     </form>
     <form  class="modelform">

    <input type="text" value="subject" name="subject"/>
    <input type="text" value="max" name="max"/>
    <input type="text" value="min" name="min"/>
      </form>
     <form  class="modelform">
    <input type="text" value="subject" name="subject" />
    <input type="text" value="max" name="max"/>
    <input type="text" value="min"  name="min"/>
   </form>

然后编写javascript代码来分配对象值

<script>
    var mainObject = $('#mainForm').serializeObject();
    var modelObject = [];
     $('.modelform').each(function(o){
        modelObject.push($(this).serializeObject());
    })
    mainObject.marks = modelObject;
</script>
于 2013-11-07T09:27:56.677 回答
1

我有

 @RequestMapping(value = "/save", method = RequestMethod.POST)
public @ResponseBody
MyEvent saveOrganization(@RequestBody Organization organization) {

    return new MyEvent('save',organization);
}

你的 mvc-servlets.xml

 <context:component-scan base-package="com.jrey.project.controllers" />
<context:annotation-config></context:annotation-config>

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

我的jQuery帖子

  $.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
    if (o[this.name] !== undefined) {
        if (!o[this.name].push) {
            o[this.name] = [ o[this.name] ];
        }
        o[this.name].push(this.value || '');
    } else {
        o[this.name] = this.value || '';
    }
});
return o;

};

  <script>
$(document)
        .ready(
                function() {
                    $('.submit', '#form')
                            .click(
                                    function() {
                                        var data = JSON
                                                .stringify($('#form')
                                                        .serializeObject());
                                        console.log(data);

                                        $
                                                .ajax({
                                                    type : "POST",
                                                    url : '${pageContext.request.contextPath}/controller/organization',
                                                    data : data,
                                                    dataType : 'json',
                                                    contentType : 'application/json;charset=UTF-8',
                                                    success : function(data) {
                                                        $(
                                                                '<div>'
                                                                        + data.message
                                                                        + '</div>')
                                                                .dialog(
                                                                        {
                                                                            title : 'Organizacion',
                                                                            modal : true,
                                                                            buttons : {
                                                                                'Aceptar' : function() {
                                                                                    document.location.href = data.location;
                                                                                }
                                                                            }
                                                                        }

                                                                );
                                                    },
                                                });
                                    });
                });

于 2013-11-06T18:27:29.730 回答