Javascript 如何将数据从vert.x服务器发送到js脚本?
我有一个vert.x服务器和一个html页面,可以用ajax发送post请求。我设法发送post请求,但我不知道如何从服务器获得答案。我的代码:Javascript 如何将数据从vert.x服务器发送到js脚本?,javascript,java,jquery,ajax,vert.x,Javascript,Java,Jquery,Ajax,Vert.x,我有一个vert.x服务器和一个html页面,可以用ajax发送post请求。我设法发送post请求,但我不知道如何从服务器获得答案。我的代码: $.ajax({ type: 'POST', url: 'http://localhost:9999', data: {d:"hi"} }).done(function(data) {alert("succes " + data);}) .fail(function(data) { ale
$.ajax({
type: 'POST',
url: 'http://localhost:9999',
data: {d:"hi"}
}).done(function(data) {alert("succes " + data);})
.fail(function(data) { alert("fail");})
顶点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"
}
}
});
但当我使用浏览器连接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);
public void start()引发异常{
httpServer=vertx.createHttpServer();
public void start()引发异常{
httpServer=vertx.createHttpServer();
requestHandler(新处理程序(){
@凌驾
公共无效句柄(HttpServerRequest){
System.out.println(“传入请求!”);
if(request.method()==HttpMethod.POST){
HttpServerResponse=request.response();
响应
.end();
}
}
});
httpServer.listen(9999);
我发现了我的错误,我用错了vert.x。。。
仍然感谢您抽出时间来回答Aleksey Bykov。Ty谢谢您的回答,是的,我想做一些更具体的事情。在我的html页面中,我有一个按钮,当我单击时,我继续发送post请求,然后我希望服务器回答我,最后我希望回调。完成以使用服务器的数据发出一个。警报。
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);