您好我试图通过ajax从jsp调用servlet,并从servlet返回两个数据。但是面对结果数据的一些问题。
两个pono podt在输出端组合在一起。如何拆分相同以获得正确的数据。
$(document).ready(function() {
$('#grn').blur(function() {
$.ajax({
url : 'callserv0',
type: 'POST',
data : {
grnno : $('#grn').val()
},
success : function(data) {
alert("new");
$('#pono').val(data);
$('#podt').val(data);
return false;
}
});
});
});

<script src="http://code.jquery.com/jquery-1.10.2.js"
type="text/javascript"></script>
<script src="js/app-ajax.js" type="text/javascript"></script>
</head>
<body>
<form>
Receipt number: <input type="text" id="grn" />
PO number: <input type="text" id="pono" />
PO Date : <input type="text" id="podt" />
<input type="button" id="find" value="Find" />
</form>
</body>
&#13;
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 {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//response.setContentType("text/html");
//PrintWriter out= response.getWriter();
//out.println("i am in callserv0");
String grn = request.getParameter("grnno");
String pono = grn;
String podt = "12/12/12";
response.setContentType("text/plain");
response.getWriter().write(pono);
response.getWriter().write(podt);
}
}
&#13;
答案 0 :(得分:0)
在将值写入响应时,可以使用逗号(,)等分隔符。 然后使用JavaScript方法 split()
var str = "123,2/21/2017";
var res = str.split(",");
console.log(res[0]);
console.log(res[1]);