【发布时间】:2019-06-06 03:03:06
【问题描述】:
我需要发送一个我在发布请求中收到的令牌。我必须在 get 请求中发送令牌,我正在做的是将 post 请求返回的令牌保存在一个中,请注意在GetToken 我将其发送到控制台显示,如果显示,即,如果它正在执行分配,但是当我尝试从ObternerInmueble() 打印它时它打印为空,我不知道为什么?
这是我的代码:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders} from '@angular/common/http';
import { Inmueble } from '../modelos/inmueble';
@Injectable({
providedIn: 'root'
})
export class HostlistService {
cabecera = {
'Accept': 'application/json',
'Authorization': ""
}
parametros = {
'grant_type':'client_credentials',
'client_id': 1,
'client_secret': 'clientSecret'
}
constructor(public http: HttpClient) {
}
obtenerToken(){
return this.http.post<any>('URL',this.parametros).subscribe(
result => {
this.cabecera.Authorization=result.token_type+" "+result.access_token;
console.log(this.cabecera.Authorization); //here I can see that the value is being allocated
this.obtenerInmuebles().subscribe();
},error =>{
console.log(error);
}
);
}
obtenerInmuebles() {
console.log("Authorization-----------_>"+this.cabecera.Authorization);
return this.http.get<any>('URL',{ headers: new HttpHeaders(this.cabecera)
});
}
mostrarCabecera(){
console.log("CABECERA::::::::"+this.cabecera.Authorization);
}
}
这是他调用方法的地方:
import { Component, OnInit } from '@angular/core';
import { HostlistService } from '../servicios/hostlist.service';
import {$,jQuery} from 'jquery';
import { Inmueble } from '../modelos/inmueble';
@Component({
selector: 'app-slider',
templateUrl: './slider.component.html',
styleUrls: ['./slider.component.css']
})
export class SliderComponent implements OnInit {
inmuebles: Inmueble[] = [];
i: number=0;
url: string = "http://crm.seaconfiable.com/upload/";
constructor(private hostlistService: HostlistService) { }
ngOnInit() {
this.hostlistService.obtenerToken();
this.hostlistService.obtenerInmuebles().subscribe(
result => {
console.log("INMUEBLES",result.data);
},error =>{
console.log(error);
}
);
}
}
这是浏览器控制台的图像,您可以在其中看到 Authorization 标头被发送为空(空白):
【问题讨论】:
-
尝试在 obtenerInmuebles() 方法中将返回类型设置为 observable 然后订阅它