Making golang Gorilla CORS handler work

后端 未结 7 1286
轮回少年
轮回少年 2020-12-14 01:13

I have fairly simple setup here as described in the code below. But I am not able to get the CORS to work. I keep getting this error:

XML

7条回答
  •  囚心锁ツ
    2020-12-14 01:22

    I realize this is an old issue but nonetheless it took me 30min to get this right.

    handler = handlers.CORS(
        // handlers.AllowedMethods([]string{"GET", "POST", "PUT"}),
        handlers.AllowedHeaders([]string{"Accept", "Accept-Language", "Content-Type", "Content-Language", "Origin"}),
        // handlers.AllowedOrigins([]string{"*"}),
    )(handler)
    

    Things to note:

    • AllowedMethods does NOT need to explicitly include OPTIONS, this is part of the CORS handler
    • AllowedHeaders need to be explicitly mentioned, * is not a valid wildcard. Typical ajax libraries will send Content-Type when requesting something like application/json, so add that as well.
    • * is the default for AllowedOrigin

提交回复
热议问题