美好的一天,我建立了一个简单的应用程序,只使用令牌获取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';
答案 0 :(得分:0)
试试这个:
let headers = new Headers({
'Authentication': "bearer <space><TOKEN HERE>",
'Content-Type': 'application/json'
});