在 AngularJS 2 中对 RESTful 网络服务进行身份验证

Authentication to a RESTful web service in an AngularJS 2

我想使用一个 api 那个 return 一个 json 文件,但我需要在我的 header 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=

谁能给我解释一下?提前致谢

你会想做这样的事情。

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    

}