Добавить фильтры перед revel.PanicFilter
revel.Filters = []revel.Filter{
ValidateOrigin,
revel.PanicFilter, // Recover from panics and display an error page instead.
revel.RouterFilter, // Use the routing table to select the right Action
revel.FilterConfiguringFilter, // A hook for adding or removing per-Action filters.
revel.ParamsFilter, // Parse parameters into Controller.Params.
IpLimitFilter,
revel.SessionFilter, // Restore and write the session cookie.
revel.FlashFilter, // Restore and write the flash cookie.
revel.ValidationFilter, // Restore kept validation errors and save new ones from cookie.
revel.I18nFilter, // Resolve the requested language
HeaderFilter,
revel.InterceptorFilter, // Run interceptors around the action.
revel.CompressFilter, // Compress the result.
revel.BeforeAfterFilter, // Call the before and after filter functions
revel.ActionInvoker, // Invoke the action.
}
var ValidateOrigin = func(c *revel.Controller, fc []revel.Filter) {
if c.Request.Method == "OPTIONS" {
c.Response.Out.Header().Add("Access-Control-Allow-Origin", "*")
c.Response.Out.Header().Add("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization") //自定义 Header
c.Response.Out.Header().Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
c.Response.Out.Header().Add("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
c.Response.Out.Header().Add("Access-Control-Allow-Credentials", "true")
c.Response.SetStatus(http.StatusNoContent)
// 截取复杂请求下post变成options请求后台处理方法(针对跨域请求检测)
} else {
c.Response.Out.Header().Add("Access-Control-Allow-Headers", "Origin, Content-Type, Accept")
c.Response.Out.Header().Add("Access-Control-Allow-Origin", "*")
c.Response.Out.Header().Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
c.Response.Out.Header().Add("Content-Type", "application/json; charset=UTF-8")
c.Response.Out.Header().Add("X-Frame-Options", "SAMORIGIN")
c.Response.Out.Header().Add("Vary", "Origin, Access-Control-Request-Method, Access-Control-Request-Headers")
fc[0](c, fc[1:]) // Execute the next filter stage.
}
}
...
Поскольку ajax превращает запрос простого запроса (одиночной публикации) во вторичный запрос, то есть сначала отправляется запрос параметров, чтобы определить, разрешен ли домен, а затем отправляется сообщение реального запроса для получения результата.