[英]How to send data from a vert.x server to a js script?
我有一个vert.x服务器和一个html页面,该页面通过ajax发送发布请求。 我设法发送了发帖请求,但我不知道如何从服务器获取答案。 我的代码:
$.ajax({
type: 'POST',
url: 'http://localhost:9999',
data: {d:"hi"}
}).done(function(data) {alert("succes " + data);})
.fail(function(data) { alert("fail");})
vert.x如下:
httpServer.requestHandler(new Handler<HttpServerRequest>() {
@Override
public void handle(HttpServerRequest request) {
System.out.println("incoming request!");
if(request.method() == HttpMethod.POST){
//here i want to do: send to the html page some data
//like "hi"
}
}
});
在此链接http://tutorials.jenkov.com/vert.x/http-server.html上 ,授权者显示:
response.write("Vert.x is alive!");
response.end();
但是,当我使用浏览器在localhost:9999上连接时,这只会在html页面上显示文本。 我已经花了几个小时了,我不知道我是否做对了。
有人可以帮忙吗?
编辑:
这是我的启动方法:
public void start() throws Exception {
httpServer = vertx.createHttpServer();
public void start() throws Exception {
httpServer = vertx.createHttpServer();
httpServer.requestHandler(new Handler<HttpServerRequest>() {
@Override
public void handle(HttpServerRequest request) {
System.out.println("incoming request!");
if(request.method() == HttpMethod.POST){
HttpServerResponse response = request.response();
response
.end();
}
}
});
httpServer.listen(9999);
我发现了自己的错误,我以错误的方式使用了vert.x ...仍然感谢您的宝贵时间Aleksey Bykov。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.