2015-11-29 71 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