您好我尝试在Jetty上运行一个应该响应ajax调用的java服务器。不幸的是,当我用ajax调用它时,响应似乎是空的。当我致电http://localhost:8081/?id=something时,我得到了答案。
Java Server:
public class Answer extends AbstractHandler
{
public void handle(String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
String id = request.getParameter("id");
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.setContentLength(19+id.length());
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().write("<message>"+id+"</message>");
//response.setContentType("text/html;charset=utf-8");
response.flushBuffer();
baseRequest.setHandled(true);
}
public static void main(String[] args) throws Exception
{
Server server = new Server(8081);
server.setHandler(new Answer());
server.start();
server.join();
}
}
js和html:
<html>
<head>
<script>
var req;
function validate() {
var idField = document.getElementById("userid");
var url = "validate?id=" + encodeURIComponent(idField.value);
if (typeof XMLHttpRequest != "undefined") {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("GET", "http://localhost:8081?id=fd", true);
req.onreadystatechange = callback;
req.send(null);
}
function callback() {
if (req.readyState == 4) {
if (req.status == 200) {
var message = req.responseXML.getElementsByTagName("message")[0];
document.getElementById("userid").innerHTML = "message.childNodes[0].nodeValue";
}
}
}
</script>
</head>
<body onload="validate('foobar')">
<div id="userid">hannak</div>
</body>
</html>
我实际上不知道我在这里做错了什么。也许有人有个好主意。
问候克里斯
答案 0 :(得分:4)
您可能正在从磁盘读取HTML文件,即使用“file:///”URL。 Web浏览器只允许XmlHttpRequests到页面来自的主机,这就是您的请求不起作用的原因。
我参加了嵌入式Jetty教程并修改了代码:
<强> Answer.java:强>
package test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
public class Answer extends AbstractHandler {
public void handle(String target, Request baseRequest, HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
String id = request.getParameter("id");
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.setContentLength(19 + id.length());
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().write("<message>" + id + "</message>");
response.flushBuffer();
baseRequest.setHandled(true);
}
public static void main(String[] args) throws Exception {
Server server = new Server(8081);
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setDirectoriesListed(true);
resource_handler.setWelcomeFiles(new String[] { "index.html" });
resource_handler.setResourceBase(".");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resource_handler, new Answer() });
server.setHandler(handlers);
server.start();
server.join();
}
}
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>AJAX Test</title>
<script type="text/javascript">
var req;
function validate() {
if (typeof XMLHttpRequest != "undefined") {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("GET", "http://localhost:8081/SomeResourceThatDoesntExistSoOurTestHandlerGetsCalled?id=fd", true);
req.onreadystatechange = callback;
req.send(null);
}
function callback() {
if (req.readyState == 4 && req.status == 200) {
var message = req.responseXML.getElementsByTagName("message")[0];
document.getElementById("userid").innerHTML = message.childNodes[0].nodeValue;
}
}
</script>
</head>
<body onload="validate()">
<div id="userid">hannak</div>
</body>
</html>
这些是我对您的代码的更改:
ResourceHandler
,当没有在URL上给出路径时,该服务器会传递index.html
文件Answer
处理程序callback
函数中取消引用了消息处理,因此显示了答案消息的内容(由于硬编码的GET-URL参数,始终为fd
)将 index.html 放在基本工作目录中并再次运行Answer类 - 当您将浏览器指向http://localhost:8081/时,事情应该按照您的意愿运行。 First Jetty将index.html提供给浏览器,然后浏览器发出XHR GET请求,并在成功时将“hannak”替换为“fd”。
HTH!