我正在尝试使用Angularjs 1.4.5进行跨域请求。 但是不能取得成功。我已经配置了$ httpprovider
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$httpProvider.defaults.headers.common['Accept']= "application/json, text/plain, */*";
$httpProvider.defaults.headers.put["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8";
$httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8";
$httpProvider.interceptors.push('authenticationFailedInterceptor');
}])
但仍然没有成功。如何使用Vert.x 2.x http服务器启用CORS支持。
Vert.x 3.x支持CORS但是现在我无法升级Vert.x。
答案 0 :(得分:1)
你有没有尝试过这样的回复?
vertx.createHttpServer()
.requestHandler(function (req) {
req.response()
.putHeader("content-type", "text/plain")
.putHeader("Access-Control-Allow-Origin", "*")
.putHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
.putHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
.end("Hello from Vert.x!");
}).listen(8080);
当然,您必须根据需要对其进行修改......但更改必须在服务器中完成,至少需要这三个标题。
答案 1 :(得分:1)
启用角色的完整示例:
我们需要创建两个路径匹配器。
一个帮助启用cors和其他处理请求。
以下是启用角色。它接受所有请求并添加所有必需的标头需要以启用cors。之后,我们需要将请求移交给实际的路由匹配器来处理请求。我们的名字是secureRoutes。
RouteMatcher routeMatcher = new RouteMatcher();
routeMatcher.options(".*",new Handler<HttpServerRequest>() {
@Override
public void handle(final HttpServerRequest request) {
request.response().putHeader("Access-Control-Allow-Origin", "*");
request.response().putHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
request.response().putHeader("Access-Control-Allow-Headers", "accept, authorization, content-type, email");
request.response().end();
}
})
.all(".*",new Handler<HttpServerRequest>() {
@Override
public void handle(final HttpServerRequest request) {
request.response().putHeader("Access-Control-Allow-Origin", "*");
secureRoutes.getRouteMatcher().handle(request);
}
});
另一个路线匹配器:
public class SecureRoutes {
private static final RouteMatcher routeMatcher = new RouteMatcher();
@Inject
protected Container container;
@Inject
private SigninController signinController;
@Inject
private SignupController signupController;
@Inject
private OauthController oauthController;
@Inject
ClientNetworkSignalController clientNetworkSignalController;
public void initRoutes() {
// APP routes. they use User token for authentication
routeMatcher.get("/", new Handler<HttpServerRequest>() {
@Override
public void handle(final HttpServerRequest request) {
request.response().putHeader("Cache-Control",
"public, max-age=86400");
request.response().sendFile("web/public/index.html");
}
});
routeMatcher.post("/signin", signinController.signin());
routeMatcher.post("/signup", signupController.signup());
routeMatcher.post("/oauth2/token", oauthController.token());
routeMatcher.post("/oauth2/invalidate_token", oauthController.invalidateToken());
}
public RouteMatcher getRouteMatcher() {
return routeMatcher;
}
}
现在最后将requestHandler添加到服务器:
server.requestHandler(routeMatcher).listen(port,
host, new Handler<AsyncResult<HttpServer>>() {
public void handle(AsyncResult<HttpServer> asyncResult) {
if (asyncResult.succeeded()) {
logger.info(s + ": Started on " + host + ":"
+ port);
} else {
logger.info(s + ": Unable to start server.\n "
+ asyncResult.cause());
}
}
});
您可能有一个问题http选项类型请求处理程序的用途是什么。答案是非常有趣的。 Javascript是一种安全语言,不允许跨源Http请求。因此,为了允许跨源请求javascript发送每个http请求请求的选项类型请求并检查天气是否支持CORS。在这样的Javascript命中服务器中,支持或不支持两次检查cors,一次支持数据。