【问题标题】:how can I assign the values to an httpheaders header in angular如何将值分配给 angular 中的 httpheaders 标头
【发布时间】:2019-06-05 17:26:44
【问题描述】:

我解释一下。我正在向一个带有一些参数的 api 发布请求发出请求,它返回一个令牌,我必须将其包含在我稍后执行的 get 请求中。到现在为止还挺好。问题是当我要获取请求时,在该请求的标头中,我必须包含我在 POST 请求中获得的 access_token 和 type_token,因为我声明了 Angular 中指定的 HttpHeaders 类型的 httpOptions 对象文档,但我不能让它工作。我究竟做错了什么?在这里我留下我到目前为止所做的代码:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders} from '@angular/common/http';
import { Inmueble } from '../modelos/inmueble';

@Injectable({
  providedIn: 'root'
})
export class HostlistService {


  httpOptions = {
    headers: new HttpHeaders({
      'Accept': '',
      'Authorization': ''

    })
  }

    parametros = {
      'grant_type':'client_credentials',
      'client_id': 1,
      'client_secret': 'clientSecret'
    }


  constructor(public http: HttpClient) {

  }

  obtenerToken(){
    return this.http.post<any>('URL/oauth/token',this.parametros).subscribe(
      result => {
        this.httpOptions.set('Accept','result.token_type');
        this.httpOptions.set('Authorization','result.access_token');
      },error =>{
        console.log(error);
      }
    );
  }

  obtenerInmuebles(){
    return this.http.get('URL',this.httpOptions);
  }

}

【问题讨论】:

    标签: angular http get


    【解决方案1】:

    尝试从结果中删除引号:

        this.httpOptions.set('Accept',result.token_type);
        this.httpOptions.set('Authorization',result.access_token);
    

    【讨论】:

      猜你喜欢
      • 2018-05-28
      • 2021-04-18
      • 2019-12-21
      • 1970-01-01
      • 2018-06-02
      • 2016-02-03
      • 2021-09-27
      • 2020-04-25
      • 1970-01-01
      相关资源
      最近更新 更多