0

I have a form in JSP like below and I want to save this list to database using AJAX call.

<form:form id="cntrForm" name="cntrForm" modelAttribute="cargoContainer" >
   <table>
       <tbody>
           <c:forEach var="container" items="${contaner.containerList}" varStatus="idx">
            <tr>
               <td>
                   <input class="text" name="containerList[${idx.index}].containerNo"  
                               value="<c:out value='${container.containerNo}'/>"/>
               </td>
               <td>
                    <input class="text" name="containerList[${idx.index}].sealNo" 
                               value="<c:out value='${container.sealNo}'/>" /> 
              </td>
           </tr>
          </c:forEach>
      </tbody>
   </table>
</form:form>

AJAX: I am trying to serialize the form and send data through AJAX as below. But I can't serialize the form. What other way can it be possible?

function saveContainer(){
     $.fn.serializeObject = function()
     {  
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name]) {
                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;
     }; 

    $.ajax({
        url : saveContainerUrl, 
        type : 'POST',
        data :  $("#cntrForm").serializeObject() ,
        success : function(data) {
            alert('Containers Saved.');
        }
    }); 
}

For your info: I am using Java, Spring, JSP

0 Answers0