当前位置:网站首页>beego跨域问题解决方案-亲试成功

beego跨域问题解决方案-亲试成功

2022-07-05 10:18:00 lookNo施

filter.go

var success = []byte("SUPPORT OPTIONS")

var corsFunc = func(ctx *context.Context) {
    
	origin := ctx.Input.Header("Origin")
	ctx.Output.Header("Access-Control-Allow-Methods", "OPTIONS,DELETE,POST,GET,PUT,PATCH")
	ctx.Output.Header("Access-Control-Max-Age", "3600")
	ctx.Output.Header("Access-Control-Allow-Headers", "X-Custom-Header,accept,Content-Type,Access-Token")
	ctx.Output.Header("Access-Control-Allow-Credentials", "true")
	ctx.Output.Header("Access-Control-Allow-Origin", origin)
	if ctx.Input.Method() == http.MethodOptions {
    
		// options请求,返回200
		ctx.Output.SetStatus(http.StatusOK)
		_ = ctx.Output.Body(success)
	}
}

func init() {
    
	beego.InsertFilter("/*", beego.BeforeRouter, corsFunc)
}

在app.conf中添加

copyrequestbody = true

POST数据在this.Ctx.Input.RequestBody中获取

var num1 map[string]interface{
    }
err := json.Unmarshal(this.Ctx.Input.RequestBody,&num1)
原网站

版权声明
本文为[lookNo施]所创,转载请带上原文链接,感谢
https://blog.csdn.net/apple_56973763/article/details/125580124