没有jwt令牌当它是具有angular2的外部源时在头中授权

时间:2017-11-17 19:10:01

标签: angular http header authorization jwt

我有这个代码(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)

this.url = '/'(本地请求)时,我在标头中有授权: enter image description here

this.url = 'http://212.227.201.82/'时,授权令牌消失。

enter image description here

如何为外部请求添加标头授权? 谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

我已经成立了这个问题! 它不是来自前端,而是来自后端!必须在API中启用CORS(跨源资源共享)

EG与ExpressJS:

$ npm install cors

简单用法(启用所有CORS请求)

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!'})
})

参考:Allow multiple CORS domain in express js / How to allow CORS? / https://github.com/expressjs/cors