简体   繁体   中英

Angular HTTPResponse header Authorization is null

I am trying to get the authentication token from the login response but the value is null and I think that only Content-Type attribute is not null...

Login method

login(credentials: any): Observable<any> {
    return this.http.post(AUTH_API + '/login', {
      username: credentials.username, 
      password: credentials.password
    }, {observe: 'response'});
  }
this.authService.login(this.loginForm.value)
      .subscribe((res:Response) => {
        console.log(res.headers.get('Authorization'));
    },
    err => {
      this.isLoginError = true;
      this.loginErrorMessage = err.error;
    });

响应标头

EDIT:

I added Access-Control-Expose-Headers to my backend response and it works.

response.setHeader("Access-Control-Expose-Headers", "Authorization");
            response.addHeader("Authorization", jwtTokenProvider.generateToken(auth));

Seems that you have to add " Access-Control-Expose-Headers " to the response headers in your backend side.

Response from Angular 5 HTTP Client has "Authorization" missing from the Response header

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM