不是让表单提交您的请求,而是让您的 checkForm 例程使用 ajax 分别进行调用?然后,您可以将结果组合到您正在执行的任何显示中。记得让 checkForm 返回 false;
我注意到您尚未接受答案,而且我的答案含糊不清。如果您想要从两个来源收集数据,其中一个使用 GET,另一个使用 POST,您可以这样做。我包括一个使用 ajax 的示例 javascript。当您单击按钮时,checkForm 函数将发送一个 POST 请求,并在完成后向第二个服务发送一个 GET。结果可以组合起来,在用户看来,它就像是一次操作。这是工作代码(当然,您必须使其适应您的服务)。
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Form</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript">
var postData = null;
$(document).ready(function() {
$("#formDiv").show();
$("#tableDiv").hide();
});
function checkForm()
{
postData = $("#Search_Form").serialize();
alert(postData);
$.ajax({
type: "POST",
url: "http://localhost:8080/FirstAjaxJspTest/AjaxService",
data: postData, // form's input turned to parms
contentType: "text",
success: function(data)
{
tableBuild(data);
},
complete: function(data) {
nextAjaxCall();
},
failure: function(msg)
{
alert("Failure: " + msg);
}
});
return false;
}
function nextAjaxCall()
{
$.ajax({
type: "GET",
url: "http://localhost:8080/FirstAjaxJspTest/AjaxService",
data: postData, // form's input turned to parms
contentType: "text",
success: function(data) {
tableBuild(data);
tableDisplay();
},
failure: function(msg) {
alert("Failure: " + msg);
}
});
return false;
}
function tableBuild(data)
{
data.forEach(function(entry) {
$("#resultsTable").append($("<tr><td>" + entry.name + "</td><td>" + entry.address + "</td></tr>"));
});
return;
}
function tableDisplay()
{
$("#formDiv").hide();
$("#tableDiv").show();
return;
}
</script>
</head>
<body>
<div id="formDiv">
<form id="Search_Form">
Name:<br/>
<input type="text" id="name" name="name" /><br/>
SSN:<br/>
<input type="text" id="ssn" name="ssn" /><br/><br/>
<button type="button" onclick="return checkForm()">Submit</button>
</form>
</div>
<div id="tableDiv">
<table id="resultsTable">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
</thead>
</table>
</div>
</body>
</html>