Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于跨域解决的疑问 #18

Open
liulinPro opened this issue Aug 12, 2020 · 1 comment
Open

关于跨域解决的疑问 #18

liulinPro opened this issue Aug 12, 2020 · 1 comment

Comments

@liulinPro
Copy link

	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 liulinPro changed the title 关于跨域解决的问题 关于跨域解决的疑问 Aug 12, 2020
Repository owner deleted a comment from liulinPro Aug 12, 2020
@8treenet
Copy link
Owner

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 8treenet reopened this Oct 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants