본문 바로가기

Web/JQuery

[jQuery] ajax 함수 사용해서 form 데이타 넘기기

 


 

 ajax 데이터를 넘기는 페이지 소스

<form name="multiForm" id="multiForm" method="post">
 <table>
  <tr>
   <td>
    <input type="text" size="15" name="mul_input" value="1" />
    <input type="text" size="15" name="mul_input" value="2" />
    <input type="text" size="15" name="mul_input" value="3" />
    <input type="text" size="15" name="mul_input" value="4" />
   </td>
  </tr>
  <tr>
   <td align="right">
    <input type="button" id="mul_input_submit" name="mul_input_submit" value="submit"/>
   </td>
  </tr>
 </table>
< /form>

 


 

★ Ajax Source

$(document).ready(function() {
 $("#mul_input_submit").click(function() {
  var formData = $("#multiForm").serialize();
  $.ajax({
   type : "POST",
   url : getAjax.jsp",
   cache : false,
   data : formData,
   success : onSuccess,
   error : onError
  });
 });
});

function onSuccess(json, status){
 alert($.trim(json));
}
function onError(data, status){
 alert("error");
} 

 


Java 단 처리

 

View 단에서 받은 값들을 request에 ParameterName을 가지고 와서 Enumeration에 값을 담는다

 

  Enumeration paramNames = req.getParameterNames();


  while (paramNames.hasMoreElements()) {
   serviceProperty = null;
   serviceProperty = new TbServiceProperty();
   
   // pName 항목 세팅
   paramName = (String) paramNames.nextElement();
   serviceProperty.setpName(paramName);
   
   String[] paramValues = req.getParameterValues(paramName);


   if (paramValues.length == 1)
    serviceProperty.setpValue(paramValues[0]);
   else {
    for (int i = 0; i < paramValues.length; ++i) {
     if (i > 0) {
      //
     }
     //serviceProperty.setpValue(paramValues[i]);
    }
   }
   listServiceProperty.add(serviceProperty);
  }