gpt4 book ai didi

javascript - Golang/ReactJS CORS 问题

转载 作者:行者123 更新时间:2023-12-01 22:36:34 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





CORS Error: “requests are only supported for protocol schemes: http…” etc

(1 个回答)


1年前关闭。




当我尝试使用 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:8080http://localhost:3000

关于javascript - Golang/ReactJS CORS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61741976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com