2017-02-21 128 views
1

嗨,我想從jsp通過ajax調用servlet,並從servlet返回兩個數據。但是面臨結果數據的一些問題。通過ajax將多個數據從servlet傳遞給jsp

兩個pono podt在輸出時組合在一起。如何分割以獲得正確的數據。

$(document).ready(function() { 
 
\t $('#grn').blur(function() { 
 
\t \t $.ajax({ 
 
\t \t \t url : 'callserv0', 
 
\t \t \t type: 'POST', 
 
\t \t \t data : { 
 
\t \t \t \t grnno : $('#grn').val() 
 
\t \t \t }, 
 
\t \t \t success : function(data) { 
 
\t \t \t \t alert("new"); 
 
\t \t \t \t $('#pono').val(data); 
 
\t \t \t \t $('#podt').val(data); 
 
\t \t \t \t return false; 
 
\t \t \t } 
 
\t \t }); 
 
\t }); 
 
});
<script src="http://code.jquery.com/jquery-1.10.2.js" 
 
\t type="text/javascript"></script> 
 
<script src="js/app-ajax.js" type="text/javascript"></script> 
 
</head> 
 
<body> 
 
<form> 
 
\t \t Receipt number: <input type="text" id="grn" /> 
 
\t \t PO number: <input type="text" id="pono" /> 
 
\t \t PO Date : <input type="text" id="podt" /> 
 
\t \t <input type="button" id="find" value="Find" /> 
 
\t </form> 
 
</body>

import java.io.IOException; 
 
import java.io.PrintWriter; 
 

 
import javax.servlet.ServletException; 
 
import javax.servlet.http.HttpServlet; 
 
import javax.servlet.http.HttpServletRequest; 
 
import javax.servlet.http.HttpServletResponse; 
 

 
public class callserv0 extends HttpServlet { 
 
\t private static final long serialVersionUID = 1L; 
 

 
\t protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
 
\t { 
 
\t \t //response.setContentType("text/html"); 
 
\t \t //PrintWriter out= response.getWriter(); 
 
\t \t //out.println("i am in callserv0"); \t 
 
\t \t 
 
\t \t String grn = request.getParameter("grnno"); 
 
\t \t \t \t 
 
\t \t String pono = grn; 
 
\t \t 
 
\t \t String podt = "12/12/12"; 
 
\t \t 
 
\t \t response.setContentType("text/plain"); 
 
\t \t response.getWriter().write(pono); 
 
\t \t response.getWriter().write(podt); 
 
\t } 
 

 
}

enter image description here

回答

0

你可以把分隔符如逗號(,)當你寫的值來響應。 然後使用JavaScript方法split()

var str = "123,2/21/2017"; 
var res = str.split(","); 
console.log(res[0]); 
console.log(res[1]); 
+0

謝謝!快速幫助。其作品!。接受安納塞爾。 – selv

+0

@selv請注意是否有用。 –