【发布时间】:2018-03-16 09:56:32
【问题描述】:
我正在尝试通过以下方式从我的角度服务获取请求来获取用户信息。
import { Injectable } from '@angular/core';
import {homeUrls} from "../../../utils/urls";
import {Http, RequestOptions, Response} from '@angular/http';
import { Headers } from '@angular/http';
@Injectable()
export class AdsService {
private headers = new Headers({'Content-Type': 'application/json'});
private token: string;
constructor(private http: Http) {
this.token = localStorage.getItem('token');
console.log("token is " , this.token);
this.headers.set('Authorization', 'Token ' + this.token );
console.log("header is");
console.log(this.headers);
this.getMe();
}
getMe(): Promise<any> {
return this.http.get(homeUrls.User, {headers: this.headers})
.toPromise()
.then(res=> {
console.log("user is");
console.log(res.json());
})
}
但我收到以下错误
无法加载 http://127.0.0.1:8000/users/account/me/:预检响应包含无效的 HTTP 状态代码 401。
当我签入网络选项卡时,它显示“未提供身份验证凭据”,尽管我已经设置了授权标头,因为它可以在我的服务的构造函数中看到。知道这里有什么问题吗?
【问题讨论】:
标签: angular angular2-services angular4-httpclient