问题描述
我有这个代码(angular2):
let headers = new Headers({'Content-Type':'应用/ JSON'});
headers.append('Authorization',this.authService.currentUser.token)
let options = new RequestOptions({headers:headers});
返回this.http.get(this.url +'quote /',options)
当 this.url ='/'
(本地请求)时,我在头部授权:
当 this.url ='http://212.227.201.82/'$c $ c>,授权令牌消失。
我如何包含标题外部请求的授权?
感谢您的帮助
我已经创立了这个问题!
它不是来自前端,而是后端!必须在API中启用(跨源资源共享)
与ExpressJS的EG:
$ npm安装cors
$简单用法(启用所有CORS请求)
c $ c $ var $ express $ require $('express')
var cors = require('cors')
var app = express()
app.use(cors ))
$ b app.get('/ products /:id',function(req,res,next){
res.json({msg:'这是为所有人启用的CORS })
})
参考: / /
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
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
这篇关于当它是带有angular2的外部源时,不在标题中授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!