如何将数据从vert.x服务器发送到js脚本?

时间:2021-01-12 18:06:00

i have a vert.x server and a html page wich send a post request with ajax. I manage to send the post request but i don't understand how to get the answer from the server. My code:

我有一个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");})

And the vert.x is as follow:

而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"
        }
    }
});

On this link http://tutorials.jenkov.com/vert.x/http-server.html, the autor shows :

在此链接http://tutorials.jenkov.com/vert.x/http-server.html上,autor显示:

response.write("Vert.x is alive!");
response.end();

But this only displays text on a html page when i connect on localhost:9999 with a browser. I have been on this for hours and i don't know if i do it right.

但是当我使用浏览器连接localhost:9999时,这只会在html页面上显示文本。我已经在这几个小时了,我不知道我是否做得对。

Can someone help plz ?

有人可以帮忙吗?

EDIT:

编辑:

Here my start method:

这是我的开始方法:

     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);

2 个解决方案

#1


1  

I've found my mistakes, i was using the vert.x the wrong way... Still thank you for your time Aleksey Bykov.

我发现了我的错误,我正在以错误的方式使用vert.x ...仍然感谢你的时间Aleksey Bykov。

#2


0  

Try to use this code instead:

请尝试使用此代码:

public class VertXServer extends Verticle {
    public void start() {
        vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
            public void handle(final HttpServerRequest httpServerRequest) {
                if(httpServerRequest.method() == HttpMethod.POST) {

                    httpServerRequest.exceptionHandler(new Handler<Throwable>() {
                        public void handle(Throwable throwable) {
                            throwable.printStackTrace();
                        }
                    });

                    httpServerRequest.dataHandler(new Handler<Buffer>() {
                        public void handle(Buffer buffer) {
                            System.out.println(buffer);
                        }
                    });

                    httpServerRequest.endHandler(new VoidHandler() {
                        protected void handle() {
                            httpServerRequest.response().end("hi");
                        }
                    });
                }
            }
        }).listen(9999);
    }
}

#1


1  

I've found my mistakes, i was using the vert.x the wrong way... Still thank you for your time Aleksey Bykov.

我发现了我的错误,我正在以错误的方式使用vert.x ...仍然感谢你的时间Aleksey Bykov。

#2


0  

Try to use this code instead:

请尝试使用此代码:

public class VertXServer extends Verticle {
    public void start() {
        vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
            public void handle(final HttpServerRequest httpServerRequest) {
                if(httpServerRequest.method() == HttpMethod.POST) {

                    httpServerRequest.exceptionHandler(new Handler<Throwable>() {
                        public void handle(Throwable throwable) {
                            throwable.printStackTrace();
                        }
                    });

                    httpServerRequest.dataHandler(new Handler<Buffer>() {
                        public void handle(Buffer buffer) {
                            System.out.println(buffer);
                        }
                    });

                    httpServerRequest.endHandler(new VoidHandler() {
                        protected void handle() {
                            httpServerRequest.response().end("hi");
                        }
                    });
                }
            }
        }).listen(9999);
    }
}