我想使用一个返回json文件的api,但我需要在我的标题HTTP中进行身份验证我不知道如何真正做到这一点,
我的typeScript服务代码是:
import {Http} from 'angular2/http';
import 'rxjs/add/operator/map';
import{Injectable} from 'angular2/core';
@Injectable()
export class PostService{
constructor(private _http: Http){
}
getPosts(){
return this._http.get("http://jsonplaceholder.typicode.com/posts").map(res => res.json());
}
}
和我的应用代码:
import {Component} from 'angular2/core';
import {PostService} from './post.service';
import { Observer } from 'rxjs/Observer';
import {Input} from 'angular2/core';
import {HTTP_PROVIDERS} from 'angular2/http';
@Component({
selector: 'my-app',
template: `
<h1>get webservice</h1>
`,
providers:[PostService, HTTP_PROVIDERS]
})
export class AppComponent {
constructor(private _postService: PostService){ this._postService.getPosts().subscribe(posts => console.log(posts));
}
}
and my api link is like :
header HTTP :
key : Authorization
the contents : Basic VXNlcjE6cGFzc3dvcmQ=
有人可以向我解释一下吗?谢谢你提前
答案 0 :(得分:0)
你会想要做这样的事情。
import {Http,Headers} from 'angular2/http';
@Injectable()
export class PostService{
//add auth data to headers
let headers = new Headers();
headers.append('Some Auth Key', 'Some Auth Value');
//then you can make your http request like you normally would
}