Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep getting "Response to preflight request doesn't pass access control check" #61

Closed
DogAndHerDude opened this issue Sep 5, 2018 · 1 comment

Comments

@DogAndHerDude
Copy link

I'm sure I'm just doing something wrong. Something completely wrong.

However, here's the issue:

var AllowedHeaders = []string{
	"Range",
	"Accept",
	"Content-Type",
	"Authorization",
	"X-CA-Session",
	"X-Requested-With",
}

func Init(session *mgo.Session) http.Handler {
	router := mux.NewRouter()

	router.Handle("/", notImplementedHandler()).Methods("GET")
	router.Handle("/api", notImplementedHandler()).Methods("GET")
	router.Handle("/api/stats/push", statsapi.NewStatsPushHandler(session)).Methods("GET", "OPTIONS")
	router.Handle("/api/interactions/push", interactionsapi.NewInteractionPushHandler(session)).Methods("POST", "OPTIONS")

	corsHandler := cors.New(cors.Options{
		AllowCredentials: true,
		AllowedOrigins:   []string{"*"},
		AllowedMethods:   []string{"GET", "POST", "OPTIONS"},
		AllowedHeaders:   AllowedHeaders,
		Debug:            true, // Enable Debugging for testing, consider disabling in production
	})

	return corsHandler.Handler(router)
}

The server keeps returning status 502, with the error message:

Failed to load '<my_server>': Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<the_origin>' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Is the config all wrong, or the server has a mind of its own?

The request to the server is relatively simple:

    const request: Request = new Request(`${process.env.STATS_API}/api/stats/push`, {
      method: 'GET',
      headers: new Headers({
        Accept: '*/*',
        'Content-Type': 'application/json',
        'X-CA-Session': sessionHashStr,
      }),
    });

    await fetch(request);

There's also a POST request, that actually sends data through in the body as well, however, haven't gotten to that point yet.

Am I missing something?

@DogAndHerDude
Copy link
Author

Alright, scratch this. Apparently the deployed server just refused connections itself. Had to be reconfigured.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant