OPTIONS都是请求put之前浏览器自动发出的。
以下截图为新版edge的截图(chrome看不到options了)。
func init() {
s := g.Server()
s.BindMiddlewareDefault(func(r *ghttp.Request) {
r.Response.CORSDefault()
r.Middleware.Next()
})
s.Group("/api", func(group *ghttp.RouterGroup) {
group.GET("areas", funcIndex)
group.GET("areas/{id}", funcShow)
group.POST("areas", funcStore)
group.PUT("areas/{id}", funcUpdate)
group.DELETE("areas/{id}", funcDestroy)
// 无论哪个版本 注册的OPTIONS请求都进不去
group.OPTIONS("areas/{id}", func(r *ghttp.Request) {
panic("can't get here")
})
})
}
路由注册:
1.11.5:
2020-03-17 13:40:07.246 200 "OPTIONS http localhost:9528 /api/areas/1?access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJJcEFkZHJlc3MiOiIiLCJJc3N1ZVRpbWVzdGFtcCI6MTU4NDQyMzQzMCwiTW9iaWxlIjoxNTU5ODI1OTkyMiwiVGVhbUlkIjoxLCJVc2VySWQiOjEzfQ.etDkPMAABIy9o4ML3DArmZNEFpBRUUaQiQQqDUuBag8 HTTP/1.1" 0.000, [::1], "http://localhost:8080/", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36 Edg/80.0.361.66"
1.11.6:
2020-03-17 13:40:32.518 404 "OPTIONS http localhost:9528 /api/areas/1?access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJJcEFkZHJlc3MiOiIiLCJJc3N1ZVRpbWVzdGFtcCI6MTU4NDQyMzQzMCwiTW9iaWxlIjoxNTU5ODI1OTkyMiwiVGVhbUlkIjoxLCJVc2VySWQiOjEzfQ.etDkPMAABIy9o4ML3DArmZNEFpBRUUaQiQQqDUuBag8 HTTP/1.1" 0.000, [::1], "http://localhost:8080/", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36 Edg/80.0.361.66"