问题
I am having this code (angular2) :
let headers = new Headers({'Content-Type': 'application/json'});
headers.append('Authorization', this.authService.currentUser.token)
let options = new RequestOptions({ headers: headers });
return this.http.get(this.url + 'quote/' , options)
when this.url = '/' (local request), I have Authorization in header:
When this.url = 'http://212.227.201.82/', Authorization token disappear.
How Can I include headers Authorization for external request? Thanks for your Help
回答1:
I have founded the issue! It is not from Front end, but backend! CORS (Cross-origin resource sharing) must be enabled in the API
EG with ExpressJS:
$ npm install cors
Simple Usage (Enable All CORS Requests)
var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())
app.get('/products/:id', function (req, res, next) {
res.json({msg: 'This is CORS-enabled for all origins!'})
})
Reference: Allow multiple CORS domain in express js / How to allow CORS? / https://github.com/expressjs/cors
来源:https://stackoverflow.com/questions/47357672/no-jwt-token-authorization-in-header-when-it-is-an-external-source-with-angular2