添加vue http标头时出错

时间:2015-11-29 02:56:57

标签: go cors vue.js

我的工作应用程序的前端位于vuejs,后端位于go。我正在将最终功能添加到身份验证中,而且我遇到了错误。

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

这是我添加到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属性,一切正常。我甚至尝试用OPTIONS处理go请求,但我的api处理程序甚至没有被击中。

1 个答案:

答案 0 :(得分: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

相关问题