第一个 - Jason 是绝对正确的,你在这种情况下想要的是 AJAX,下面是一个正在运行的示例。
第二个 - 您应该使用诸如 jQuery 之类的 Javascript 库,这可能看起来很吓人(就像一开始对我所做的那样),但它真的很容易并且完全值得花一点点努力来实现它。
3 - 使用 jQuery,您的应用程序花絮应该看起来像这样,使用您提供的示例:
HTML -
<p>
<label for="badge">ID #:</label>
<input id="badge" name="ID" type="text" pattern="[0-9]{6}"
placeholder="xxxxxx">
// Please note that I removed the onClick section from the line below.
<button id="checkButton" type="button">Enter</button>
</p>
JQUERY -
// The default function you described to take information and display it.
function showInfo(data) {
// Insert your function here, probably using JSON as the Content Type
}
// This is the key AJAX function, using jQuery, that takes your info and gets a
// response from the server side, the sends it to the function above in order for it
// to be displayed on the page.
function processIdInfoCheck() {
$.ajax({
type: 'post',
url: '/http://localhost:8080/testapp/authenticate',
data: {
'id': $('#badge').val();
},
dataType: 'json',
success: displayIdInfoReturn,
error: function () {
alert("There was an error processing your request, please try again");
}
});
}
// When the page loads, the code below will trigger, and bind your button click
// with the action you want, namely triggering the AJAX function above
(function ($) {
$('#checkButton').bind('click', processIdInfoCheck);
})(jQuery);
请记住,AJAX 需要付出一些努力才能获得所需的效果,但是当您查看页面加载时间、请求数量等时……这是完全值得的。请让我知道这是否有帮助,如果您需要任何细节。