You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gin框架配置CORS后响应未添加跨域头问题求助

Gin框架配置CORS后响应未添加跨域头问题求助

各位大佬好,我现在用Go语言的Gin框架开发接口,遇到了CORS跨域错误。之前在NodeJS项目里碰到类似问题很快就解决了,但这次用Gin怎么都搞不定,实在头疼😩。

我找到了gin-contrib/cors这个包来配置跨域,修改了代码如下:

r := gin.Default()

r.Use(cors.New(cors.Config{
    AllowOrigins:     []string{"http://localhost:5173"},
    AllowMethods:     []string{"PUT", "PATCH", "POST"},
    AllowHeaders:     []string{"Origin"},
    ExposeHeaders:    []string{"Content-Length", "Content-Type"},
    AllowFiles:       true,
    AllowCredentials: true,
    AllowOriginFunc: func(origin string) bool {
        return origin == "http://localhost:5173"
    },
}))

但配置完之后,跨域错误还是照样出现,有没有大佬能帮我看看哪里出问题了?

备注:内容来源于stack exchange,提问作者Sushant Dhiman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 15:14:50