美文网首页
Gin跨域问题

Gin跨域问题

作者: 会爬虫的小蟒蛇 | 来源:发表于2022-07-31 13:58 被阅读0次

Gin的跨域比较简单 用轮子就好

go get -u github.com/gin-contrib/cors

使用默认的跨域策略

r := gin.Default()
r.Use(cors.Default())
// 此处cors.Default 已经将基础跨域的信息配置好了

自定义跨域策略

import (
    "time"
 
    "github.com/gin-contrib/cors"
    "github.com/gin-gonic/gin"
)
 
func Cors() gin.HandlerFunc {
    c := cors.Config{
        AllowAllOrigins: true,
        AllowMethods:    []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
        AllowHeaders:    []string{"Content-Type", "Access-Token", "Authorization"},
        MaxAge:          6 * time.Hour,
    }
 
    return cors.New(c)
}

相关文章

网友评论

      本文标题:Gin跨域问题

      本文链接:https://www.haomeiwen.com/subject/kcskwrtx.html