Search code examples
node.jsangularhttprequesthttpresponse

can not access data header in build version of angular 10


here my header of code in http request:

    return this.http.post<any>(this.path.userRoot + '/logIn/random', body,
        {
            withCredentials: true,
            responseType: "json",
            observe: 'response'
        }
    )
        .pipe(map(response => {
            if (response.status === 200) {
                localStorage.setItem('auth-token', response.headers.get('auth-token'));
                localStorage.setItem('user', JSON.stringify(response.body.message));
                this.userSubject.next(response.body.message);
            }
            return response;
        }));

after run, the value of "response.headers.get('auth-token')" is null. Is there a way to solve this problem?


Solution

  • If you use CORS, then you need to specify the Access-Control-Expose-Headers (see documentation) server side.

    Access-Control-Expose-Headers: auth-token, OtherCustomHeader