2016-10-16 3 views
5

API가 반환하는 오류 본문을 어떻게 구문 분석 할 수 있는지 궁금합니다. Angle 2의 구문 분석 본문

이 API 리턴의 이미지 : 나는 URL에 액세스 할 수 있습니다

login(username,password){ 
     let headers = new Headers(); 
     headers.append('Content-Type','application/json'); 

     return this.http.post(this.configEnvironment.url() + "oauth/access_token", 
      JSON.stringify(
       { 
        username: username, 
        password: password, 
        grant_type: "password", 
        client_id: "xxxxxxx", 
        client_secret: "xxxxxxxx" 
       } 
      ), 
      { headers } 
     ) 
     .map(res => res.json()) 
     .catch((err:any) =>{ 
      console.log(err); 
      return Observable.throw(new Error(err)); 
     }); 

    } 

, 상태,하는 statusText 등이 사용 :

err.status,err,url,error.statusText 
을 여기 enter image description here

은 내 코드입니다

내 문제는 내가 오류 본문의 가치를 얻을 수 없다는 것입니다.

답변

11

catch에는 실제로 Response이 수신됩니다. 당신은 json()

import { Response } from "@angular/http"; 
... 
.catch((err:Response) =>{ 
      let details = err.json().error; 
      console.log(details); 
      return Observable.throw(new Error(details)); 
     }); 

메모와 함께 세부 정보를 액세스 할 수 있습니다 :이 답변은 @angular/http 라이브러리에 관한 것입니다. Angular 5 현재이 라이브러리는 더 이상 사용되지 않습니다. @angular/common/http

+0

Thanks Man! 이 내 문제를 해결 –

+0

@SydneyLoteria 아무 문제 없습니다. 방금 한 것처럼'any'에서'Response'로'err'의 타입을 변경하면 더 나은 IDE 지원을받을 수 있습니다. – BeetleJuice

+0

Angular 5 @ Angular/Common/http 방법은 무엇입니까? –