Golang,cors和angularjs – 头部缺失

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

Golang, cors and angularjs - header missing

问题

我正在使用rs/cors在我的Go API中,以允许我的AngularJS应用程序进行直接请求。我已经添加了以下代码来配置CORS:

crs := cors.New(cors.Options{AllowCredentials: true})
n.Use(crs) //Negroni include

但是,当我发出请求时,浏览器显示请求的资源上没有'Access-Control-Allow-Origin'头的消息。

我的请求如下所示:

var req = {
    method: 'POST',
    url: endPoint + version + method,
    headers: {
        'Authorization': 'Basic ' + btoa(':' + appId)
    },
    data: params
}

$http(req).
    success(function(data, status, headers, config) {
        callback(null, data);
    }).
    error(function(data, status, headers, config) {
        callback(data);
    });

如何解决这个问题?

英文:

I'm using rs/cors in my Go API to allow my Angularjs app to make direct requests. I've added the following code to configure CORS:

crs := cors.New(cors.Options{AllowCredentials: true})
n.Use(crs) //Negroni include

but I'm getting the No 'Access-Control-Allow-Origin' header is present on the requested resource message in the browser when I make a request.

My request looks like this:

var req = {
    method: 'POST',
    url: endPoint + version + method,
    headers: {
        'Authorization': 'Basic ' + btoa(':' + appId)
    },
    data: params
}
      
$http(req).
    success(function(data, status, headers, config) {
        callback(null, data);
    }).
    error(function(data, status, headers, config) {
        callback(data);
    });

How can I get round this?

答案1

得分: 3

修复了!我注意到请求中发送了几个不同的头部,我必须明确地允许它们。

AllowedHeaders: []string{"accept", "authorization", "content-type"}

希望这对某人有所帮助。

英文:

Fixed it! I noticed that a few different headers were being sent with the request and I had to explicitly allow all of them.

AllowedHeaders: []string{"accept", "authorization", "content-type"}

I hope this helps someone.

huangapple
  • 本文由 发表于 2015年5月30日 01:41:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/30535867.html
匿名

发表评论

匿名网友

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

确定