我实现了一些java函数。现在我必须通过html将结果呈现给用户。
public Object post()
{
responseHeaders.put("Content-Type", "application/json");
try
{
final User user = UC0_Login.getLoggedInUser(this);
List<Measurement> mylist = MeasurementService.getMeasurementsbyPatient(user.getUsername().toString());
for(int i = 0; i < mylist.size(); i++) {
Measurement mesPatient = mylist.get(i);
DownloaderService.saveTxt(mesPatient);
// System.out.println("Test Name: " + mesPatient.getRequest().getPatient().getFirst_name().toString());
}
}
catch(HttpException x)
{
x.printStackTrace();
this.statusCode = x.getStatusCode();
return "{ \"success\": false }";
}
catch(Exception x)
{
x.printStackTrace();
this.statusCode = StatusCode.SERVER_ERROR_500;
return "{ \"success\": false }";
}
String property = "java.io.tmpdir";
String tempDir = System.getProperty(property);
String result = "Your measurements have been saved at" + tempDir;
return result;
}
我在Java类中实现了post()。我还在后台创建了一个HttpServer。如代码所示,我返回一个String。如何在HTML中打印出该字符串?我如何从HTML获取输入(例如整数)到例如get()函数?谢谢!!!
答案 0 :(得分:0)
如果你基本上在寻找AJAX请求和响应,这里有一个很好的资源来学习如何。
https://blog.garstasio.com/you-dont-need-jquery/ajax/#posting
例如,
HTML
<p id="result"></p>
<form id="frm1" action="/action_page.php">
Your input: <input type="text" id="myInput"><br><br>
<input type="button" onclick="myFunction()" value="Submit">
</form>
JAVASCRIPT
var input = document.getElementById("myInput").value;
xhr = new XMLHttpRequest();
xhr.open('POST', 'Your URL Goes Here');
xhr.onload = function() {
if (xhr.status === 200) {
var response = xhr.responseText;
document.getElementById("result").innerHTML = response;
}
else if (xhr.status !== 200) {
alert('Request failed. Returned status of ' + xhr.status);
}
};
xhr.send(encodeURI('input=' + input)); // Use your API param keys here