为什么将标题添加到角度请求会导致它根本无法正常工作

时间:2019-07-14 19:20:14

标签: angular angular-httpclient

我目前正在尝试向有角度的HTTP请求中添加标头,但是我不明白为什么为什么在尝试添加标头时既没有设置标头,也没有发送有效载荷...

const accessToken = this.discordService.getAccessToken()
const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type': 'application/json',
    'Authorization': accessToken
  })
};
console.log('access token', accessToken)
this.http.post(environment.functionsUrl + '/setup', JSON.stringify(
    { userId: userId })).subscribe(r => console.log(r));

这导致有效负载被传递,但是未设置“授权”标头:

enter image description here

但是,当我在其中添加标题时

const accessToken = this.discordService.getAccessToken()
const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type': 'application/json',
    'Authorization': accessToken
  })
};
console.log('access token', accessToken)
this.http.post(environment.functionsUrl + '/setup', JSON.stringify(
    { userId: userId }), httpOptions).subscribe(r => console.log(r));

没有“有效载荷”部分,并且标题仍未设置:

enter image description here

角度版本:

    "@angular/animations": "^7.2.13",
    "@angular/cdk": "^7.3.7",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/fire": "^5.1.2",
    "@angular/forms": "~7.2.0",
    "@angular/material": "^7.3.7",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",

0 个答案:

没有答案