Beego框架如何设置Access-Control-Allow-Origin
How to set Access-Control-Allow-Origin in Beego framework
我正在开发 RESTFul API 在服务器端使用 Beego 框架并在客户端使用 AngularJS。服务器和客户端都在我的笔记本电脑中(仍在开发中)。 127.0.0.1:8000 上的客户端 运行 和 127.0.0.1:8080 上的服务器。
当我尝试访问端点(使用 AngularJS $http 服务)时,出现以下错误:
XMLHttpRequest cannot load http://127.0.0.1:8080/v1/products/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8000' is therefore not allowed access.
我知道我必须在 beego 上设置这个 CORS 东西。不幸的是,在搜索 Google 之后,我得到的唯一答案是来自官方网站 (in the comment section),这对我来说还不够清楚。有什么建议吗?我应该写什么样的代码,放在Beego的什么地方?
您想通过在导入语句中包含 "github.com/astaxie/beego/plugins/cors" 来使用 cors 插件。
包中有一个注释掉的示例,如下所示:
func main() {
// CORS for https://foo.* origins, allowing:
// - PUT and PATCH methods
// - Origin header
// // - Credentials share
beego.InsertFilter("*", beego.BeforeRouter,cors.Allow(&cors.Options{
AllowOrigins: []string{"https://*.foo.com"},
AllowMethods: []string{"PUT", "PATCH"},
AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
}))
beego.Run()
}
因此,如果您希望任何人都能够访问您的后端,请将 AllowOrigins 参数更改为 []String{"*"}。并且大概在 AllowMethods 参数中包含 "GET" 。
如何在Beego框架中设置Access-Control-Allow-Origin
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
AllowAllOrigins: true,
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
AllowCredentials: true,
}))
我正在开发 RESTFul API 在服务器端使用 Beego 框架并在客户端使用 AngularJS。服务器和客户端都在我的笔记本电脑中(仍在开发中)。 127.0.0.1:8000 上的客户端 运行 和 127.0.0.1:8080 上的服务器。
当我尝试访问端点(使用 AngularJS $http 服务)时,出现以下错误:
XMLHttpRequest cannot load http://127.0.0.1:8080/v1/products/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8000' is therefore not allowed access.
我知道我必须在 beego 上设置这个 CORS 东西。不幸的是,在搜索 Google 之后,我得到的唯一答案是来自官方网站 (in the comment section),这对我来说还不够清楚。有什么建议吗?我应该写什么样的代码,放在Beego的什么地方?
您想通过在导入语句中包含 "github.com/astaxie/beego/plugins/cors" 来使用 cors 插件。
包中有一个注释掉的示例,如下所示:
func main() {
// CORS for https://foo.* origins, allowing:
// - PUT and PATCH methods
// - Origin header
// // - Credentials share
beego.InsertFilter("*", beego.BeforeRouter,cors.Allow(&cors.Options{
AllowOrigins: []string{"https://*.foo.com"},
AllowMethods: []string{"PUT", "PATCH"},
AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
}))
beego.Run()
}
因此,如果您希望任何人都能够访问您的后端,请将 AllowOrigins 参数更改为 []String{"*"}。并且大概在 AllowMethods 参数中包含 "GET" 。
如何在Beego框架中设置Access-Control-Allow-Origin
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
AllowAllOrigins: true,
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
AllowCredentials: true,
}))