我们如何使用在角度2中具有令牌的API?
考虑下面的例子..
root url:http://awesomedommain.com/test/api/mytest/ 令牌:ZfGwMSgzMDo4ODkyZDgyNS1jZTQDLFs0SGEtOTNlOS04ZTI1AWI4SW332Dd0c2Fm
我如何在http中使用它?
答案 0 :(得分:0)
这是你在Angular 2中传递令牌的方法。
let headers = new Headers({ 'Content-Type': 'application/json', 'Token': 'ZfGwMSgzMDo4ODkyZDgyNS1jZTQDLFs0SGEtOTNlOS04ZTI1AWI4SW332Dd0c2Fm' });
let options = new RequestOptions({ headers: headers });
this._http.post("api/YourApi", formData, options);
答案 1 :(得分:0)
您可以在app.service.ts
:
request() {
let headers = new Headers({
'Token': "ZfGwMSgzMDo4ODkyZDgyNS1jZTQDLFs0SGEtOTNlOS04ZTI1AWI4SW332Dd0c2Fm",
'Content-Type': 'application/json'
});
return this.http.get("http://awesomedommain.com/test/api/mytest/", { headers: headers })
.map(
res => res.json()
)
.catch(
err => Observable.throw(err || "Server error")
);
}
要执行请求,您需要注入Angular Http服务和Headers,如下所示:
import { Http, Headers } from '@angular/http';
并在你的构造函数中:
constructor(
private http: Http
) { }