有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javascript向vert发出ajax post请求。x服务器

我想制作一个小代码,只需向服务器发送POST请求,并在服务器应答时打印“hi”。 我有一个ajax函数,可以实现对vert的post请求。x服务器:

$.ajax({
        type: 'POST',
        url: 'http://localhost:9999',
        data: {message:tmpMessage},
        async:false
    }).done(function() {alert("succes");})
    .fail(function() { alert("fail");})
    .always(function() { alert("complete");});

这个java代码是一个vert。x服务器,当他收到POST请求时,只会返回一个响应

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

但它从不进入回调函数。完成(函数(){aler(“success”);}我不明白为什么。我认为这不是使用HttpServerResponse的方法,但我在任何地方都找不到合适的答案。有人能帮忙吗


共 (1) 个答案

  1. # 1 楼答案

    我不确定你的POST是什么类型,但即使这样你也在比较,它看起来有点可疑

    无论如何,假设您正在使用Vert。x2。x、 为此更改您的代码部分:

    if ("POST".equals(request.method())) {
      request.response().end();
    }
    

    。。。你可以走了

    注意:您可以在任何其他库/框架中找到要比较POST的类型,如常量或枚举