Golang/ReactJS CORS 麻烦
Golang/ReactJS CORS trouble
当我尝试使用 axios
发送 post 请求时在控制台中遇到此错误
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
})
似乎有几种方法可以处理此问题,但其中 none 似乎对我有用。例如,这不起作用:
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))
}
更改允许的来源:来自 http://localhost:8080 to http://localhost:3000
的值
当我尝试使用 axios
发送 post 请求时在控制台中遇到此错误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
})
似乎有几种方法可以处理此问题,但其中 none 似乎对我有用。例如,这不起作用:
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))
}
更改允许的来源:来自 http://localhost:8080 to http://localhost:3000
的值