So I am running a backend server locally, and the frontend in a Docker container at localhost:8080. When I open the front end, I get:
Access to fetch at 'http://localhost:3000/myservice' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I had a root around online and saw various versions of setting headers. None worked. The one i have now is like this:
func (m MyService) initializeRoutes() {
m.router.HandleFunc(VERSION+"/stuff", corsHandler(b.getStuff)).Methods("GET")
}
func corsHandler(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Print("preflight detected: ", r.Header)
w.Header().Add("Connection", "keep-alive")
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Methods", "POST, OPTIONS, GET, DELETE, PUT")
w.Header().Add("Access-Control-Allow-Headers", "content-type")
w.Header().Add("Access-Control-Max-Age", "86400")
// continue with my method
h(w, r)
}
}
func (m MyService) getStuff(w http.ResponseWriter, r *http.Request) {
//Do what is required
}
But i get the same error from the start no matter what i seem to set. Anyone know why I am getting this and how to fix it?