![](/img/trans.png)
[英]Angular 6 HttpClient GET response not returning the custom headers
[英]angular 2 - httpClient is returning empty headers
我正在使用httpClient執行Web服務,但是我的問題是響應頭始終為空,我已經檢查了這 一點 ,並且無法使其正常工作,這是我的服務:
iniciarSesion(usuario: Usuario) {
const idSistema = environment.idSistema;
const url = environment.url + environment.mssesiones;
return this.httpClient.post(
url,
{
usuario: usuario.usuario,
contrasena: usuario.contrasena,
idSistema: idSistema
},
{
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
observe : 'response'
});
}
這是執行服務的組件:
this.inicioSesionService.iniciarSesion(usuario).subscribe(
(httpResponse: HttpResponse<Object>) => {
console.log(httpResponse);
});
有人可以幫我解決這個問題嗎?,
提前致謝。
搜索后,我發現問題出在服務(春季啟動服務)上,我需要添加Access-Control-Expose-Headers
如下所示:
headers.add("authorization", jwtUtil.generateToken(usuario, peticion.getRemoteAddr()));
headers.add("Access-Control-Expose-Headers", "authorization");
我注意到在執行之前,httpResponse返回空的標題:
httpResponse.headers.get('authorization');
執行完之后,console.log顯示標題信息,我不了解該行為,但是現在可以正常工作了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.