我正在開發一個API,在Go和我的前端Angular(沒有AngularJS),但是當我從Angular的Web應用程序調用我的API時,在後端看不到我的標頭,特殊情況下我的授權標題,因爲我的API具有基於JWT的身份驗證。標題http沒有傳遞到我的API
另外我想提及的是,我使用Postman和Go Request客戶端來測試我的應用程序,並且我的API在我的API中交付時沒有任何問題。
下面附加的是我後端的CORS和來自我的前端的API調用。
我的後端:
func Cors() gin.HandlerFunc {
log.Println("CORS Middleware")
return func(c *gin.Context) {
c.Writer.Header().Add("Access-Control-Allow-Origin", "*")
c.Next()
}
}
我的前端:
getData() {
const auth = `Bearer ${this.token}`;
const headers = new Headers({
'Access-Control-Allow-Origin': '*',
'Accept': 'application/json',
'Authorization': auth,
});
const options = new RequestOptions(headers);
console.log(headers); //Here I can see
const products = this.http.get('localhost:8000/api/products', options)
.subscribe((response: Response) => {
this.data = response.json();
});
return products;
}
感謝和我的英語很抱歉,我認爲我的CORS會引起問題。
這可能是不相關的,但你需要的「授權」年底將逗號:AUTH, – SolidSnake