【问题标题】:Cors is blocking client access to local vertx-serverCors 阻止客户端访问本地 vertx-server
【发布时间】:2020-02-20 03:48:49
【问题描述】:

在通过 jquery 获取 JSON 对象时,我遇到了 CORS 阻止我的客户端访问 vertx 服务器的问题。我发现CORS issue in vertx Application not working 是一个解决方案,但我似乎在我的实现中做错了什么。

我正在测试的一个是 get-invokation,但我认为同样的问题适用于所有 api。

@Override
public void start() {

    Router router = Router.router(vertx);

    router.route().handler(CorsHandler.create(".*.")
            .allowedMethod(io.vertx.core.http.HttpMethod.GET)
            .allowedMethod(io.vertx.core.http.HttpMethod.POST)
            .allowedMethod(io.vertx.core.http.HttpMethod.OPTIONS)
            .allowedHeader("Access-Control-Request-Method")
            .allowedHeader("Access-Control-Allow-Credentials")
            .allowedHeader("Access-Control-Allow-Origin")
            .allowedHeader("Access-Control-Allow-Headers")
            .allowedHeader("Content-Type"));

    router.route("/api/diagrams*").handler(BodyHandler.create());
    router.post("/api/diagrams").handler(this::insert);
    router.delete("/api/diagrams/delete/:id").handler(this::delete);
    router.get("/api/diagrams/get/:id").handler(this::get);
    router.get("/api/diagrams/get/user/:username").handler(this::getByUser);

    vertx.createHttpServer()
            .requestHandler(router::accept)
            .listen(7070);
}

private void get(RoutingContext rc) {
    final String id = rc.request().getParam("id");
    if (id == null) {
        rc.response().setStatusCode(400).end();
    } else {
        UserDiagram diagram = userDiagramDao.get(id);
        if (diagram == null) {
            rc.response().setStatusCode(404).end();
        } else {
            rc.response()
                    .putHeader("content-type", "application/json; charset=utf-8")
                    // Are these necessary?
                    .putHeader("Access-Control-Allow-Origin", "*")
                    .putHeader("Access-Control-Allow-Methods", "POST, GET")
                    .putHeader("Custom-Header", "Own-Data")
                    .putHeader("Access-Control-Expose-Headers", "Custom-Header")
                    //
                    .end(Json.encodePrettily(diagram));
        }
    }
}

从 f12 看到的 Cors 错误:

jquery.min.js:4 访问 XMLHttpRequest at 'localhost:7070/api/diagrams/get/5db07cfd5e189c7a093bf920' 来自原点 'http://localhost:8080' 已被 CORS 策略阻止:跨源 请求仅支持协议方案:http、data、chrome、 铬扩展,https。发送@jquery.min.js:4 ajax@jquery.min.js:4 (匿名)@chart.xhtml:21 chart.xhtml:12 跨域读取阻塞 (CORB) 阻止跨域响应 http://registry.npmjs.org/node-fetch,MIME 类型为 application/json。 请参阅https://www.chromestatus.com/feature/5629709824032768 了解更多信息 细节。 jquery.min.js:4 [Violation] 'error' 处理程序耗时 1376 毫秒

从客户端测试查询如下:

    var result_data;
    $.ajax({
        url: "localhost:7070/api/diagrams/get/5db07cfd5e189c7a093bf920",
        type: "GET",
        //contentType: "application/json",
        //  dataType: 'json',
        data: diagram_data,
        success: function(res) {
            result_data = res;
            alert("Success!!");
        },
        error: function(xhr, textStatus, errorThrown) {
            alert("Query failed");
        }
    });

CORS issue in vertx Application not working

我也对问题出在客户端还是服务器端感到困惑。

【问题讨论】:

  • 从 REST 客户端调用 API 并检查标头。

标签: javascript java jquery cors vert.x


【解决方案1】:

尝试将http:// 放入请求网址中。

正如错误所说,请求仅支持 http、https 等协议......

【讨论】:

  • 我之前尝试过使用http,但那是在我更新后端之前,所以我不认为它是那样愚蠢的。
猜你喜欢
  • 2021-04-27
  • 2019-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多