beego使用CROS允許跨域請求
阿新 • • 發佈:2018-11-25
首先到
https://github.com/astaxie/beego/tree/master/plugins/cors
下載cros檔案
然後在main中引入cors包 在程式開始插入如下程式碼
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{ AllowAllOrigins: true, AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"}, ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"}, AllowCredentials: true, }))
引數說明
AllowAllOrigins 允許全部來源設定為true則所有域名都可以訪問本網站介面,可以將此配置換成為AllowOrigins:[“允許訪問的域名”]
AllowMethods :允許的請求型別
AllowHeaders:允許的頭部資訊
ExposeHeaders:允許暴露的頭資訊
AllowCredentials:如果設定,允許共享AuthTuffic證書,如Cookie
###結束