Alan
Alan

Reputation: 10145

no jwt token Authorization in header when it is an external source with angular2

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: enter image description here


When this.url = 'http://212.227.201.82/', Authorization token disappear.

enter image description here

How Can I include headers Authorization for external request? Thanks for your Help

Upvotes: 0

Views: 1593

Answers (1)

Alan
Alan

Reputation: 10145

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

Upvotes: 1

Related Questions