How to set Access-Control-Allow-Origin in Beego framework

huangapple go评论78阅读模式
英文:

How to set Access-Control-Allow-Origin in Beego framework

问题

我正在使用Beego框架在服务器上开发一个RESTful API,并在客户端使用AngularJS。服务器和客户端都在我的笔记本电脑上(仍在开发中)。客户端运行在127.0.0.1:8000,服务器运行在127.0.0.1:8080。

当我尝试访问一个端点(使用AngularJS的$http服务)时,我收到以下错误信息:

XMLHttpRequest无法加载http://127.0.0.1:8080/v1/products/。请求的资源上没有'Access-Control-Allow-Origin'头。因此,不允许访问来自'http://127.0.0.1:8000'的资源。

我知道我需要在beego上设置CORS相关的内容。不幸的是,在搜索了Google之后,我只找到了官方网站上的一个答案(在评论部分),但对我来说不够清晰。有什么建议吗?我应该写什么样的代码,把它放在Beego的哪个位置?

英文:

I'm developing a RESTFul API using Beego framework on the server and AngularJS on the client side. Both server and client are in my laptop (still in dev). Client run on 127.0.0.1:8000 and server on 127.0.0.1:8080.

When I try to hit an endpoint (using AngularJS $http service) I get the following error:

> 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.

I know I have to set this CORS stuff on beego. Unfortunately, after searching Google, the only answer I got was from official website (in the comment section) which is not clear enough for me. Any advice? What kind of code should I write and where to put it on Beego?

答案1

得分: 9

你想要使用cors插件,可以在导入语句中包含"github.com/astaxie/beego/plugins/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"。

英文:

You want to use the cors plugin by including "github.com/astaxie/beego/plugins/cors" in your import statement.

The package has a commented out example shown below:

 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()
 }

So if you want anyone to be able to hit your backend, change the AllowOrigins parameter to just []String{"*"}. And presumably include "GET" in the AllowMethods paramter.

答案2

得分: 0

在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,
}))

英文:

How to set Access-Control-Allow-Origin in Beego framework.

 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,
    }))

huangapple
  • 本文由 发表于 2015年1月29日 21:47:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/28216342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定