【问题标题】:no jwt token Authorization in header when it is an external source with angular2当它是具有 angular2 的外部源时,标头中没有 jwt 令牌授权
【发布时间】:2018-05-01 15:28:55
【问题描述】:

我有这个代码(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 = '/'(本地请求)时,我在标头中有授权:


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

如何包含外部请求的标头授权? 感谢您的帮助

【问题讨论】:

    标签: angular http header authorization jwt


    【解决方案1】:

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

    使用 ExpressJS 的 EG:

    $ 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

    【讨论】:

      猜你喜欢
      • 2016-03-08
      • 2017-07-01
      • 2015-09-27
      • 2016-08-04
      • 2021-07-22
      • 2015-05-26
      • 2020-07-27
      • 2017-07-02
      • 2021-02-09
      相关资源
      最近更新 更多