简体   繁体   中英

Not able to read Angular 2 http header Authorization token in Express

I am making cross-domain POST call from Angular 2 application to express server and it makes a OPTIONS call first and then POST and everything works fine.

Once I add something in header like -

headers.append('API-TOKEN', 'xxxx');

It is not making POST call after OPTIONS, even I'm not able to read 'API-TOKEN' at Express (server) code. It prints undefined.

Any help would be appreciated.

The most likely scenario is that your pre-flight call is returning an Access-Control-Allow-Headers header that doesn't include your custom API-TOKEN header in the response. I would check if that is the case and add the header to the Access-Control-Allow-Headers list of allowed headers.

Here's the documentation for that header .

Look into cors . Most probably you are trying to use an api which is on a different server that your page. You can enable cors easily using this library.

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