freedom icon indicating copy to clipboard operation
freedom copied to clipboard

关于跨域解决的疑问

Open liulinPro opened this issue 3 years ago • 1 comments

	app := freedom.NewApplication()
	app.Iris().Use(func(ctx iris.Context) {
		ctx.Header("Access-Control-Allow-Origin", "*")
		ctx.Header("Access-Control-Allow-Credentials", "true")
		ctx.Header("Access-Control-Allow-Headers", "*")
		ctx.Header("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE")

		if ctx.Method() == "OPTIONS" {
			ctx.StatusCode(http.StatusOK)
			return
		}
		ctx.Next()
	})
	app.Iris().AllowMethods(iris.MethodOptions)

这样做好像没有解决

liulinPro avatar Aug 12 '20 07:08 liulinPro

package main

func installMiddleware(app freedom.Application) {
	crs := func(ctx freedom.Context) {
		ctx.Header("Access-Control-Allow-Origin", "*")
		ctx.Header("Access-Control-Allow-Credentials", "true")

		if ctx.Method() == "OPTIONS" {
			ctx.Header("Access-Control-Allow-Headers", "Access-Control-Allow-Origin,Content-Type")
			ctx.Header("Access-Control-Max-Age", "86400")
			ctx.StatusCode(204)
			return
		}
		ctx.Next()
	}
	app.InstallMiddleware(crs)
}

8treenet avatar Oct 16 '20 11:10 8treenet