From 8314a770f1717bce49dfbe43100223abe90ed25a Mon Sep 17 00:00:00 2001 From: Mathieu Sanchez Date: Wed, 12 Jun 2019 17:33:54 +0900 Subject: [PATCH] Add CORS --- CAu_NLP_2019/src/main/java/com/nlp/App.java | 24 +++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/CAu_NLP_2019/src/main/java/com/nlp/App.java b/CAu_NLP_2019/src/main/java/com/nlp/App.java index c4cc586..d8d0b3a 100644 --- a/CAu_NLP_2019/src/main/java/com/nlp/App.java +++ b/CAu_NLP_2019/src/main/java/com/nlp/App.java @@ -11,8 +11,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import static spark.Spark.get; -import static spark.Spark.port; +import static spark.Spark.*; public class App { @@ -24,6 +23,27 @@ public class App { ArrayList results = getResults(words); return gson.toJson(results); }); + 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", "*")); } private static ArrayList getResults(String[] words) throws Exception {