From b402ebba4f9c93a0040972748d66f9c4c6b02eb7 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Wed, 27 Nov 2019 22:45:26 -0500 Subject: Add CORS headers to backend responses Access-Control-Allow-Origin:* header added to API responses to allow angular to access API. Note: This allows all domains to access this API through browser javascript See for a description of CORS --- backend/main.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/main.go b/backend/main.go index 4766de2..0a95fc8 100644 --- a/backend/main.go +++ b/backend/main.go @@ -31,6 +31,8 @@ func MakeAPIResponse(status int, msg string, data interface{}) *APIResponse { } func RecipeList(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") //Enable CORS + if r.Method == "GET" { var ids []int var id int @@ -137,6 +139,8 @@ func RecipeList(w http.ResponseWriter, r *http.Request) { } func SingleRecipe(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") //Enable CORS + recipe_id, err := strconv.Atoi(r.URL.Path[len("/recipes/"):]) if err != nil { fmt.Println("Not a valid ID") -- cgit v1.1