当我通过 jquery 获取 JSON 对象时,我遇到了 CORS 阻止客户端访问 vertx 服务器的问题。我发现vertx 应用程序中的 CORS 问题无法作为解决方案,但我似乎在实现中做错了一些事情。我测试的一个是 get-invokation,但我认为同样的问题适用于所有 api。@Overridepublic 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)); } }}
添加回答
举报
0/150
提交
取消