Serhiy
Serhiy

Reputation: 1981

How to read received headers in Angular 2?

Can some body tell me how to read received headers in Angular 2? i have mad a request, for login and password, and there should be sent back headers with Token. I need the token for further workaround.

here is part of the code:

  sendLogin(username, password) {
    let body = JSON.stringify({"username": username, "password": password});
    let headers = new Headers({'Content-Type': 'application/json'});
    let options = new RequestOptions({headers: headers});

    return this.http.post(this.loginUrl, body, options)
    .map(res => res.json())
        .map((res) => {
            if (res.ok) {
              // at least how to console.log received headers?
                console.log( res.headers); //undefined
               this.loggedIn = res.ok;
            }   return res.ok;
        });
};

thank you.

Upvotes: 1

Views: 627

Answers (1)

Thierry Templier
Thierry Templier

Reputation: 202138

Most of the time such an issue is related to CORS. You need to explicitly enable allowed headers in the response headers.

You're only be able to see the header in the map only if it's enabled by CORS.

Your server needs to return the following in headers:

Access-Control-Allow-Headers: X-SomeHeader

Upvotes: 2

Related Questions