XMLHttpRequest cannot load http://127.0.0.1:2000/swagger.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
i've tried to add "Access-Control-Allow-Origin" to response header
if origin := r.Header.Get("Origin"); origin != "" {
rw.Header().Set("Access-Control-Allow-Origin", origin)
}
rw.Header().Set("Content-Type", "application/json")
rw.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
rw.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token")
rw.Header().Set("Access-Control-Allow-Credentials", "true")
but not work!
I can't use swagger-ui & swagger_editor to load swager.json & test API
you can add a cors middleware.
https://github.com/rs/cors
http://goswagger.io/use/middleware/
Working example (in configure_name.go):
import "github.com/rs/cors"
func setupGlobalMiddleware(handler http.Handler) http.Handler {
handleCORS := cors.Default().Handler
return handleCORS(handler)
}
This is not working for me, I still get 405 Method Not Allowed. Is there a new fix for this?
405 method not allowed for what? are you sure you're requesting with the right basepath etc?
This solution has been proven to work for many people so something else must be wrong.
Most helpful comment
Working example (in configure_name.go):