2014-03-30 136 views
0

嘗試使用jQuery-AJAX將JSP數據從JSP文件發送到servlet,但在servlet post請求中獲得空值。無法使用jQuery-AJAX發送FormData從JSP頁面到Servlet

在JSP頁面上,它在控制檯顯示正確的數據,並且在發送請求時顯示它正在發送數據,我已經檢查過在Mozilla中使用螢火蟲。但是在servlet中獲得空值。

JSP碼:

 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 

     <script src="js/jquery-1.8.3.js" type="text/javascript"></script> 
     <script type="text/javascript" src="js/jquery-ui-1.8.24.js"></script> 
     <script type="text/javascript"> 
      $(document).ready(function() { 

    alert("DOM is ready"); 

}); 

      function qry() 
      { 

       var fd = new FormData();        
       var tinfo=$("#tablename").val();     
       var cinfo=$("#columnname").val(); 
       var oinfo=$("#operator").val(); 
       var uinfo=$("#txtuserinput").val(); 
       console.log(tinfo); 
       console.log(cinfo); 
       console.log(oinfo); 
       console.log(uinfo); 

       fd.append("abc",tinfo); 
      fd.append("mycolumnname",cinfo); 
      fd.append("myoperator",oinfo); 
      fd.append("mytxtuserinput",uinfo); 

       $.ajax({  

           type: 'POST', 
           url: "http://localhost:8084/NavigatingApp/getInfo/queryoutput",  
           contentType: false, 
           dataType: "html", 
           data: fd,         
     async:false,    
     cache: false, 

     processData: false,     

       success:function(data){ 
        console.log(data); 

         }, 
     error:function(){ 
      alert("Error in receving data...!!!"); 
     } 
    }); 
      } 
      </script> 
    </head> 
    <body> 
     <h1>Hello World!</h1> 
     <select name="tablename" id="tablename" > 
      <option value="tbl1">tbl1</option> 
      <option value="tbl2">tbl2</option> 
      <option value="tbl3">tbl3</option>    
     </select> 
     <select name="columnname" id="columnname"> 
      <option value="col1">col1</option> 
      <option value="col2">col2</option> 
      <option value="col3">col3</option> 
     </select> 
     <select name="operator" id="operator"> 
      <option value="op1">op1</option> 
      <option value="op2">op2</option> 
      <option value="op3">op3</option> 
     </select> 
     <select name="condition" id="condition" > 
      <option value="condtn1">condtn1</option> 
      <option value="condtn2">condtn2</option> 
      <option value="condtn3">condtn3</option> 
     </select> 
     <input type="text" name="txtuserinput" id="txtuserinput" /> 
      <input type="button" value="search" id="btnsearch" onclick="qry();" /> 
    </body> 
</html> 

servlet代碼:

所有

data: fd,

嘗試

@WebServlet(name = "queryoutput", urlPatterns = {"/queryoutput"}) 
public class QueryOutput extends HttpServlet { 
public QueryOutput() { 
     super(); 
     // TODO Auto-generated constructor stubatavu 

    } 

    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 

    } 


    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 


      response.setContentType("application/json;charset=UTF-8"); 


      System.out.println(" inside do post table::" +request.getParameter("mytablename")); 
      System.out.println(" inside do post col name:: " +request.getParameter("mycolumnname")); 
      System.out.println(" inside do post operator::" +request.getParameter("myoperator")); 
      System.out.println(" inside do post user input::" +request.getParameter("mytxtuserinput")); 
       response.setContentType("application/json;charset=UTF-8"); 

      PrintWriter out=response.getWriter(); 


       out.flush(); 

    }  
} 
+0

我遇到同樣的問題。你找到解決方案嗎? –

回答

0

您還沒有發送數據;

data: {fd: fd}

+0

如果我做數據,它不會發送ajax請求:{fd:fd} – user1244566