2015-11-29 59 views
0

我的工作应用程序前端为vuejs,后端为go。我正在将最终功能添加到身份验证过程中,并且遇到错误。添加vue http标头时出错

当我尝试添加任何http标头到vuejs请求时,我收到一个错误。

这是我加入到vue component

http: { 
    headers: { 
    Authorization: 'Bearer 123' 
    } 
} 

而且我得到一个preflight错误:

XMLHttpRequest cannot load http://localhost:8000/api/investments?limit=6. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. 

现在我知道这通常是一个服务器端的问题,但我我正在添加响应标题resp.Header().Set("Access-Control-Allow-Origin", "*")cors。如果我删除vue object中的http属性,一切正常。它的漂亮bizzare我甚至尝试用go处理OPTIONS请求,但我的api处理程序甚至没有被击中。

回答

0

好的,所以我发现答案是服务器问题Authorization头未被授权。因此,只需将其添加为中间件即可。

c := cors.New(cors.Options{ 
     AllowedHeaders: []string{"Origin", "Accept", "Content-Type", "Authorization"}, 
    }) 
handler := c.Handler(router) 
log.Fatal(http.ListenAndServe(":8000", handler)) 

我现在用的是rs/cors

+0

随时接受你自己的答案。 – holys

+0

@holys我得等2天 – Rodrigo