2016-08-15 4 views
0

Ich versuche, die WEB-API von http.get zugreifen, aber es funktioniert nicht das ist der Dienst, in dem ich Observable benutze, aber es funktioniert überhaupt weder Aufruf zum Server und nicht angezeigt irgendein Fehler auch.Observables funktioniert nicht angular 2 auf webapi

@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); 
    } 
} 

Antwort

1

beobachtbar sind kalt, so dass Sie subscribe

Musterservice implmentation wie this-

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

Von Ihrer Komponente Sie this- wie

this._webApiService.getEmployees().subscribe(
       emplist => this.employees = emplist, 
       error => this.errorMessage = <any>error 
       ); 
abonnieren

Sehen Sie, ob das hilft.

+0

danke für die Hilfe, die ich nicht abonniert habe – rashfmnb