gin 框架
gin 框架解决跨域问题
package main
import (
"gin_es7/es"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"time"
)
func Atime(t string) string {
//fmttime := "2020-08-14 00:00:00"
localtime, _ := time.ParseInLocation("2006-01-02 15:04:05", t, time.Local)
return localtime.UTC().Format("2006-01-02T15:04:05.000Z")
}
func main() {
r := gin.Default()
newCors := cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"POST, GET, OPTIONS, PUT, DELETE"},
AllowHeaders: []string{"Origin", "Authorization", "Content-Type"},
ExposeHeaders: []string{"Content-Type"},
AllowOriginFunc: func(origin string) bool {
return true
},
//超时时间设定
MaxAge: 24 * time.Hour,
})
r.Use(newCors)
r.GET("/fcn-data/resptime", func(c *gin.Context) {
stime := c.DefaultQuery("stime", "2020-08-13 00:00:00")
dtime := c.DefaultQuery("dtime", "2020-08-27 00:00:00")
demain := c.DefaultQuery("demain", "api.yungehuo.com")
api := c.DefaultQuery("api", "yungehuo-account/companysupplierrelation")
esstime := Atime(stime)
esdtime := Atime(dtime)
c.JSON(200, es.RespTimeQuery(demain, api, esstime, esdtime, stime, dtime))
})
r.Run("0.0.0.0:8392") // listen and serve on 0.0.0.0:8080
}
测试后台接口有没有跨域
有token
var token= "LtSFVqKxvpS1nPARxS2lpUs2Q2IpGstidMrS8zMhNV3rT7RKnhLN6d2FFirkVEzVIeexgEHgI/PtnynGqjZlyGkJa4+zYIXxtDMoK/N+AB6wtsskYXereH3AR8kWErwIRvx+UOFveH3dgmdw1347SYjbL/ilGKX5xkoZCbfb1f0=,LZkg22zbNsUoHAgAUapeBn541X5OHUK7rLVNHsHWDM/BA4DCIP1f/3Bnu4GAElQU6cds/0fg9Li5cSPHe8pyhr1Ii/TNcUYxqHMf9bHyD6ugwOFTfvlmtp6RDopVrpG24RSjJbWy2kUOOjjk5uv6FUTmbrSTVoBEzAXYKZMM2m4=,R4QeD2psvrTr8tkBTjnnfUBw+YR4di+GToGjWYeR7qZk9hldUVLlZUsEEPWjtBpz+UURVmplIn5WM9Ge29ft5aS4oKDdPlIH8kWNIs9Y3r9TgH3MnSUTGrgayaNniY9Ji5wNZiZ9cE2CFzlxoyuZxOcSVfOxUw70ty0ukLVM/78=";
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://127.0.0.1:8081/thc_bpm/actionLog/test');
xhr.setRequestHeader("x-access-token",token);
xhr.send(null);
xhr.onload = function(e) {
var xhr = e.target;
console.log(xhr.responseText);
}
无token
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://127.0.0.1:8081/thc_bpm/actionLog/test');
xhr.send(null);
xhr.onload = function(e) {
var xhr = e.target;
console.log(xhr.responseText);
}

浙公网安备 33010602011771号