2016-09-24 12 views
1

私のhttpコールは200に戻りますが、応答は取り込まれません。 購読内のコードがヒットしていません。私が郵便配達所でテストするとき、APIはデータを返しています。ここに私のコードです。角2のHTTPポストは200を返していますが、応答は返されません。

getToken(authcode: string) { 

     var data = 'client_id=InspectWebApp_client&code=' + authcode + '&redirect_uri=http://localhost:3000&grant_type=authorization_code'; 
     let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' }); 
     let options = new RequestOptions({ headers: headers }); 
     this.http.post('https://fedloginqa.test.com/as/token.oauth2', data, options) 
      .subscribe((res: Response) => { 

       var resultsToken = res.json(); 
       localStorage.setItem("access_token",resultsToken.access_token) 
       //return this.inspections; 
      }) 

    } 

答えて

2

私も同じ問題に直面していました。 Observablesのmap関数を使用して問題を解決しました。ここに私の実装があります:

login(Username:string, Password:string) : Observable<Response>{ 
    let headers = new Headers(); 
    headers.append("Authorization", "Basic " + btoa(Username + ":" + Password)); 
    headers.append("Content-Type", "application/x-www-form-urlencoded"); 
    return this._http.post(this._baseUrl+"auth/login", " " , {headers: headers} ) 
     .map((response: Response) => { 
      return response;  
     }).catch(this.handleError); 
} 

ここで、handleErrorは生成された例外をキャッチする関数です。これはlogin.service.tsの関数で、ユーザー名とパスワードをapiに送信してデータを取得します。このサービスのマップ機能から応答を返すことがわかります。今、返されたレスポンスは、サブスクライブ関数で次のように捕捉されます。

this._loginService.login(this.username, this.password) 
     .subscribe(
      (response) => { 
       //Here you can map the response to a type. 
       this.apiResult = <IUser>response.json(); 
      }, 
      (err) => { 
       //Here you can catch the error 
      }, 
      () => {this.router.navigate(['home'])} 
     ); 
関連する問題