Angular 使用.pipe()以角度检索响应头

Angular 使用.pipe()以角度检索响应头,angular,typescript,angular-httpclient,Angular,Typescript,Angular Httpclient,我有一个从API获取HTTP响应的方法。我正在调用的API现在已经更改,并将我需要的字符串放入HTTP头中 目前,此代码正常工作,不会返回错误: // Create a Visitor JWT Token createVisitor() { // Create the visitor to send to API // TODO: Capture the params of the URL and not the full URL this.visitor = {

我有一个从API获取HTTP响应的方法。我正在调用的API现在已经更改,并将我需要的字符串放入HTTP头中

目前,此代码正常工作,不会返回错误:

// Create a Visitor JWT Token
  createVisitor() {
    // Create the visitor to send to API
    // TODO: Capture the params of the URL and not the full URL
    this.visitor = {
      BrandCode: 'honey',
      QueryString: 'example=qstring'
    };

    // Set Token as Session & Return a Success/Fail
    return this.http.post(this.api + this.controller + 'createvisitor', this.visitor).pipe(
      map((response: any) => {
        console.log(response);
      })
    );
  }
但是,当我在控制台中查看日志时,它只是将响应输出为一个没有标题的字符串。我在guard中使用的方法的订阅部分:

// Create the token...
this.visitorService.createVisitor().subscribe(next => {
}, error => {
  console.log('Create Token Error');
});

我是否需要将本地存储从.pipe()方法移动到该方法的subscribe部分,或者在.pipe()中是否有我可以执行此操作的方法?

您可以在管道中执行本地存储代码,如下所示

     createVisitor() {
        // Create the visitor to send to API
        // TODO: Capture the params of the URL and not the full URL
        this.visitor = {
          BrandCode: 'honey',
          QueryString: 'example=qstring'
        };
    
        // Set Token as Session & Return a Success/Fail
        return this.http.post(this.api + this.controller + 'createvisitor', this.visitor, {observe: 'response'}).pipe(
          map((response: any) => {
            console.log(response.headers.keys());// all header names
 console.log(response.body); // response content

          })
        );
      }

不完全一样-如果可以,我想避免在subscribe中设置会话,我想尝试从.pipe()中进行设置,但不确定这是否可行?确定它是可能的:
pipe(map(x=>//做你的事))
x只是以字符串的形式出现——我需要的是响应的标题,而不是主体。在订阅中,您可以在管道中执行任何操作。您的问题是有角度的,除非您根据@enno.void中的建议问题通过{observe:'response'},否则不会显示标题。另外,如果标题是非CORS安全的标题,您也会遇到问题,除非您将其添加到服务器端的Access Control Expose Headers@cjd82187我明白您的意思-因此部分问题似乎是Auth标题没有公开,因为我可以取回另一个-谢谢!我需要一个标题而不是正文“response”只返回正文我编辑了答案,请检查这是否适用于u。