In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "how to solve Golang gin cross-domain problems". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to solve Golang gin cross-domain problems" can help you solve your doubts.
Gin cross-domain solution cors1.gopackage middlewaresimport ("github.com/gin-gonic/gin"net/http") func Cors () gin.HandlerFunc {return func (c * gin.Context) {method: = c.Request.Method origin: = c.Request.Header.Get ("Origin") if origin! = "{ C.Header ("Access-Control-Allow-Origin" Origin) / / mainly set Access-Control-Allow-Origin c.Header ("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") c.Header ("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept") Authorization ") c.Header (" Access-Control-Expose-Headers "," Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type ") c.Header (" Access-Control-Allow-Credentials "," false ") c.Set (" content-type ") "application/json")} if method = = "OPTIONS" {c.AbortWithStatus (http.StatusNoContent)} c.Next ()} cors2.gofunc Cors () gin.HandlerFunc {return cors.New (cors.Config {AllowAllOrigins: false, AllowOrigins: nil AllowOriginFunc: func (origin string) bool {return true}, AllowMethods: [] string {"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"}, AllowHeaders: [] string {"Authorization", "ts", "Accept", "Origin", "DNT" "X-CustomHeader", "Keep-Alive", "User-Agent", "X-Requested-With", "If-Modified-Since", "Cache-Control", "Content-Type", "Content-Range", "Range"}, AllowCredentials: true, MaxAge: 10 * time.Minute )} using middleware package routerimport ("github.com/gin-gonic/gin"goproejct/controllers"goproejct/middlewares" / / introducing middleware goproject is the project name according to your own situation) func InitRouter () {router: = gin.Default () router.Use (Cors ()) / / using middleware v1: = router.Group ("v1") {v1.POST ("/ login") Controllers.Login) v1.POST ("/ regist", controllers.Regist)} router.Run (": 8000")} here This article "how to solve the cross-domain problem of Golang gin" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it. If you want to know more about the article, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.