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

一个人想着一个人 提交于 2019-12-11 16:15:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!