2016-08-15 2 views
0

http.get에서 웹 API에 액세스하려고합니다.이 서비스는 작동하지만 observable을 사용하고 있지만 서버를 호출하지 않고 표시하지도 않습니다. 어떤 오류도. 당신이 subscribeobservables webapi에서 앵귤러 2를 작동하지 않습니다

샘플 서비스 implmentation 호출 할 필요가 있도록 관찰 가능한

@Injectable() 
export class SetupServiceObservables { 

    public apiServiceBaseUri = 'http://localhost:3000/'; 

    private authheaders: any = {}; 
    private config: any = {}; 
    private auth: any = {}; 

    constructor(private http: Http, private ngWEBAPISettings: WebAPISettings, private authService: AuthService, private router: Router) { 

    } 

    public get(servicename: string): Observable<any> { 
     return this.http.get(this.ngWEBAPISettings.apiServiceBaseUri + "api/" + servicename + "/", this.getConfig()) 
      .map(this.extractData) 
      .catch(this.handleError);; 
    } 

    public getConfig() { 
     this.authheaders.Authorization = this.auth.access_token == "" ? "" : 'Bearer ' + this.auth.access_token; 
     this.config = { 
      headers: { 
       'Authorization': this.authheaders.Authorization 
      }, 
      cache: false, 
     }; 
     return this.config; 
    } 



    private extractData(res: Response) { 
     let body = res.json(); 
     console.log(res); 
     return body.data || {}; 
    } 


    private handleError(error: any) { 
     let errMsg = (error.message) ? error.message : 
      error.status ? `${error.status} - ${error.statusText}` : 'Server error'; 
     console.error(errMsg); // log to console instead 
     return Observable.throw(errMsg); 
    } 
} 

답변

1

는 this-

this._webApiService.getEmployees().subscribe(
       emplist => this.employees = emplist, 
       error => this.errorMessage = <any>error 
       ); 
처럼 구독 할 수 있습니다 구성 요소에서 this-

getEmployees(): Observable<{}> { 
     return this._http.get(this._webApiUrl) 
      .map((response: Response) => <any[]> response.json()) 
      .catch(this.handleError) 
      ; 
    } 

처럼, 감기

도움이되는지 확인하십시오.

+0

도움을 주셔서 감사합니다. 구독하지 않았습니다. – rashfmnb

관련 문제