你好,我正在构建一个简单的应用程序,它只获取带有令牌的api数据,但我有未经授权的访问错误…
有人能帮我弄明白为什么我会犯这个错误吗?
未经授权的401人
import { Component, ViewEncapsulation } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
encapsulation: ViewEncapsulation.None,
})
export class AppComponent {
data: any = {};
constructor( private http: Http ) { }
ngOnInit() {
this.getVoicepicData();
this.getData();
}
getData(){
let headers = new Headers({
'Token': "TOKEN HERE",
'Content-Type': 'application/json'
});
return this.http.get('http://apiURL.com/api/voice/get-voice-pick-
file/', { headers: headers })
.map((res: Response) => return res.json())
}
getVoicepicData() {
this.getData().subscribe(data => {
console.log(data)
this.data = data
})
}
}
我如何处理这个错误?有人能帮我弄清楚吗?我是否正确设置了令牌?
编辑:
缺少
import { Headers } from '@angular/http';
最佳答案
试试这个:
let headers = new Headers({
'Authentication': "bearer <space><TOKEN HERE>",
'Content-Type': 'application/json'
});