Created
March 8, 2016 10:30
-
-
Save saeidzebardast/e375b7d17be3e0f4dddf to your computer and use it in GitHub Desktop.
Enable CORS in Spark Java to allow origins *
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
options("/*", | |
(request, response) -> { | |
String accessControlRequestHeaders = request | |
.headers("Access-Control-Request-Headers"); | |
if (accessControlRequestHeaders != null) { | |
response.header("Access-Control-Allow-Headers", | |
accessControlRequestHeaders); | |
} | |
String accessControlRequestMethod = request | |
.headers("Access-Control-Request-Method"); | |
if (accessControlRequestMethod != null) { | |
response.header("Access-Control-Allow-Methods", | |
accessControlRequestMethod); | |
} | |
return "OK"; | |
}); | |
before((request, response) -> response.header("Access-Control-Allow-Origin", "*")); |
Thank's !
Thanks.
Hello,
I try to execute Spark-submit from Angular post http and given problem CROS:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://185.28.5.2:4200' is therefore not allowed access.
Any help plaise
@JavaSparker no problem with the following configuration
BasicConfigurator.configure();
Spark.staticFiles.location("/assets");
Spark.staticFiles.header("Access-Control-Allow-Origin", "*");
options("/*", (req, res) -> {
String accessControlRequestHeaders = req.headers("Access-Control-Request-Headers");
if (accessControlRequestHeaders != null) {
res.header("Access-Control-Allow-Headers", accessControlRequestHeaders);
}
String accessControlRequestMethod = req.headers("Access-Control-Request-Method");
if (accessControlRequestMethod != null) {
res.header("Access-Control-Allow-Methods", accessControlRequestMethod);
}
return "OK";
});
before((req, res) -> {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "*");
res.type("application/json");
});
ilysm
Love you! haha
thanks!
Thank you :)
thanks it's working
Thank you!!!
thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks