1-й - Джейсон абсолютно прав, что в этой ситуации вам нужен AJAX, ниже приведен пример в движении.
2-й - вам следует использовать библиотеку Javascript, например jQuery , что может показаться пугающим (как это было поначалу для меня), но это действительно легко и полностью стоит небольших усилий, чтобы начать работу.
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 требует определенных усилий, чтобы получить желаемый эффект, но когдаВы смотрите на время загрузки страницы, номера запросов и т. д. Это того стоит.Пожалуйста, дайте мне знать, если это было полезно, и если вам нужны какие-либо подробности.