登录
首页 >  Golang >  Go问答

困扰着的Golang和ReactJS的跨域问题

来源:stackoverflow

时间:2024-03-28 23:51:25 206浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《困扰着的Golang和ReactJS的跨域问题》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

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

从源“http://localhost:3000”访问“localhost:8080”处的 xmlhttprequest 已被 cors 策略阻止:跨源请求仅支持协议方案: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:8080更改为http://localhost:3000

以上就是《困扰着的Golang和ReactJS的跨域问题》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>