我用的是Go Gin Frameworkgin

func CORSMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        c.Writer.Header().Set("Content-Type", "application/json")
        c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
        c.Writer.Header().Set("Access-Control-Max-Age", "86400")
        c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
        c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, X-Max")
        c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")

        if c.Request.Method == "OPTIONS" {
            c.AbortWithStatus(200)
        } else {
            c.Next()
        }
    }
}

我有状态代码:200 OK,但OPTIONS请求后没有任何react . 看起来我错过了什么,但是我不明白我哪里错了.

有人能帮我吗?

推荐答案

FWIW,这是我的CORS中间件,可以满足我的需求.

func CORSMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
        c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
        c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
        c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")

        if c.Request.Method == "OPTIONS" {
            c.AbortWithStatus(204)
            return
        }

        c.Next()
    }
}

Go相关问答推荐

从Kafka到Clickhouse的实时消费数据

运行add. inf,这样我们就可以在app.conf中使用. inf参数了?

[0]Func()as";请勿比较哨兵类型

../golang/pkg/mod/github.com/wmentor/lemmas@v0.0.6/processor.go:72:9:未定义:令牌.进程

如何将GoFr筛选器用于查询参数?

在Mac中使用uname获取处理器体系 struct 时,在为AMD64构建Go二进制时出现错误结果

租户GUID X的租户不存在self 邮箱帐户的租户(我是唯一的成员)

mockgen不创建模拟

如何使用Gio设置标题栏图标

如何使用gopher-lua定义一个Lua函数,该函数有一个预定义的表作为param,Lua脚本可以在其中访问该函数中的表?

在Go中旋转矩阵

Golang Gorm Fiber / argon2.Config 未定义

Go 中的sync.Cond 与 Wait 方法

gopacket:IP-in-IP 数据包上的解码层

具有两个参数的动态规划:天数和优惠券

Go:从 ssl 证书中获取 'subject/unstructeredName' 的值

Golang grpc go.mod 问题

K8s 算子读取原始数据

在 Golang 中使用 OR 条件验证 struct 的两个字段

如何迭代在泛型函数中传递的片的并集?