获取401状态,甚至为每个请求传递应用程序ID和密钥
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded',});
let options = new RequestOptions({ headers: headers });
options.headers.set('Accept', "application/json,text/xml ");
options.headers.set('X-AYLIEN-NewsAPI-Application-ID', "ff6**d33");
options.headers.set('X-AYLIEN-NewsAPI-Application-Key', "b7445d942********7c06e");
API调用:
this.http.get(url)
.subscribe(res => {
resolve(res.json());
}, (err) => {
reject(err);
});
});
}
这是我用来在标题中设置值的代码,但它不起作用。
答案 0 :(得分:1)
getDataGetAuth(url) {
return new Promise((resolve, reject) => {
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({ headers: headers });
options.headers.set('Content-Type', 'application/json' );
options.headers.set('Authorization', 'Bearer '+ sessionStorage.getItem("token") );
this.http.get( MainURL+url,options)
.subscribe(res => {
resolve(res.json());
}, (err) => {
reject(err);
});
});
}
此代码对我来说很好用
答案 1 :(得分:0)
尝试以下代码:
服务中:
第一:
从'@ angular / common / http'导入{HttpHeaders};
private url = "your_url";
private headers: any;
this.headers = new HttpHeaders({ 'Content-Type': 'application/json',
'your_key': '' + value, 'your_key': '' + value });
getRecords(): Promise<any>{
const url = `${this.url}/end_point`;
return this.http.get(url, {headers: this.headers })
.toPromise()
.catch(this.handleError);
}
在Component中消耗您的服务API调用,例如:
this.your_service_obj.getRecords()
.then(response => {
// console.log(response)
},
err => {
})