【问题标题】:Golang/ReactJS CORS trouble [duplicate]Golang/ReactJS CORS 问题 [重复]
【发布时间】:2020-05-12 00:58:06
【问题描述】:

当我尝试使用 axios 发送发布请求时在控制台中遇到此错误

Access to XMLHttpRequest at 'localhost:8080' from origin 'http://localhost:3000' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

axios.post('localhost:8080', { username }).then(res => {
    // Do stuff
  })

似乎有几种方法可以处理这个问题,但它们似乎都不适合我。例如,这不起作用:

func main() {

    router := mux.NewRouter()

    router.HandleFunc("/username", models.CheckUsername).Methods("POST", "OPTIONS")

    c := cors.New(cors.Options{
        AllowedOrigins:   []string{"http://localhost:3000"},
        AllowCredentials: true,
    })

    handler := c.Handler(router)

    log.Println("Listening on port ", port)
    log.Fatal(http.ListenAndServe(port, handler))
}

【问题讨论】:

  • AllowedOrigins 应该是http://localhost:3000
  • 也试试axios.post('http://localhost:8080', ...

标签: javascript reactjs go cors mux


【解决方案1】:

将 AllowedOrigins:值从 http://localhost:8080 更改为 http://localhost:3000

【讨论】:

  • 试过这个,效果一样。编辑了问题。
  • 你用哪个包做cors? github.com/rs/cors ?
  • 是的,就是这个
  • 搞定了。会接受这一点,因为这是问题的一部分。谢谢。
  • @Cody 剩下的问题是什么?我也面临同样的问题
猜你喜欢
  • 2020-09-15
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 2022-12-18
  • 2020-03-01
  • 2021-03-17
  • 2016-09-20
相关资源
最近更新 更多