Skip to content

Instantly share code, notes, and snippets.

@saeidzebardast
Created March 8, 2016 10:30
Show Gist options
  • Save saeidzebardast/e375b7d17be3e0f4dddf to your computer and use it in GitHub Desktop.
Save saeidzebardast/e375b7d17be3e0f4dddf to your computer and use it in GitHub Desktop.
Enable CORS in Spark Java to allow origins *
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", "*"));
@vaghi
Copy link

vaghi commented May 29, 2019

Love you! haha

@activated
Copy link

thanks!

@chiquito83
Copy link

Thank you :)

@saravanasksk
Copy link

thanks it's working

@TheFlemoid
Copy link

Thank you!!!

@J0sueTM
Copy link

J0sueTM commented Oct 5, 2022

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment