设置一个中间件,对请求头进行改变
r := gin.Default()
r.Use(cors.New(cors.Config{
AllowOriginFunc: func(origin string) bool { return true },
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
r.GET("/posts/index", func(c *gin.Context) {
// c.JSON:返回JSON格式的数据
c.HTML(http.StatusOK, "get/index.tmpl", nil)
})
r.Run()