【发布时间】:2019-01-06 22:13:54
【问题描述】:
我在 java 中托管一个 http 网络服务器(只是 localhost)来显示一个离线网站(纯 html)。要将数据发送回 java,保存按钮使用 ajax 调用 post/form 数据。
我正在使用 com.sun.net.httpserver.HttpServer;
并且希望能够在处理程序中获取 xml 字符串。
我尝试了几种映射功能,并在网上找到了示例。没有任何帮助。到目前为止,他们都发现了空结果。
我的目标是只选择 xml 变量,其他变量无关紧要。 (不需要迭代搜索/解析器)
提前致谢! 埃里克
Javascript 代码:
<script type ="text/javascript">
function saveFile() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
};
xhttp.open("POST", "/", true);
//xhttp.setRequestHeader("Accept", "text");
xhttp.setRequestHeader("content-type", "multipart/form-data");
formData.append('xml', '<xml>some xml thing here</xml>');
xhttp.send(formData);
}
</script>
java handler code:
private static void handleRequest(HttpExchange exchange) throws IOException {
String root = "TestPlanDesigner";
URI uri = exchange.getRequestURI();
//System.out.println(uri);
//printRequestInfo(exchange);
//if its the root, this means its posting the data
if(uri.getPath().equals("/"))
{
if(exchange.getRequestMethod().equals("POST"))
{
//handle the post request.
String response = "File saved successfully";
exchange.sendResponseHeaders(200, response.getBytes().length);
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
单行在处理程序内选择java中的xml表单数据。
【问题讨论】: