繁体   English   中英

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

[英]Authentication to a RESTful web service in an AngularJS 2

我想使用一个返回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=

有人能解释一下吗? 提前致谢

你会想做这样的事情。

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    

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM