Angular2 http fetch request with header failed

I am trying to execute a simple receive request via angular2 http as follows: (the token is present and retrieved from the message in my api)

let idToken = localStorage.getItem('id_token');
let authHeader = new Headers();
if (idToken) {
    authHeader.append('Authorization', 'Bearer ' + idToken);
}
return this._http.get('http://someapicall-to-my-custom-api', {headers: authHeader})
.map(response => response.json())
.subscribe(
    data => console.log(data),
    error => console.log(JSON.stringify(error)),
    () => console.log('Completed')
);

If I call my api without headers, it returns a good result. At the moment when I add the title, I am looking at the error in the subscription. The problem is that I need to start sending a header with it in order to get some protected data, and still it does not work.

In the console, I do not see any useful information at all, I just see this:

{"_body":{"isTrusted":true},"status":200,"ok":true,"statusText":"Ok","headers":{},"type":3,"url":null}

In the GET request on api, I see no 200 ok, but I do when I did not add headers. There are also no response headers.

If I try to use the postman to do the same, it works without problems in this program.

, - . ? ?

+4
3

Access-Control-Allow-Headers: Authorization

. CORS: Access-Control-Allow-Headers

+2

route.php API Laravel, api, , . Günter Zöchbauer, , :)

header('Access-Control-Allow-Headers:  Content-Type, X-Auth-Token, Origin, Authorization');
+1

, .

, . ( - ). URL- URL- , , URL-.

, -, URL-. , .

0
source

All Articles