2017-09-14 1 views
-1

저는 Angular2 v4.4.0beta를 실행하고 있습니다. 나는 객체를 반환하려고 애 쓰고 내 API의 자식입니다. 엔드 포인트를 호출하고 여기에 JSON입니다 : 여기자식 배열 반환 오류가있는 Angular2 json 객체

{ 
    "companyId":3, 
    "name":"Third", 
    "description":null, 
    "characters":[{ 
     "characterId":3, 
     "name":"Third", 
     "description":null, 
     "status":null, 
     "companyId":3, 
     "userId":null 
    }] 
} 

이 구성 요소에서 내 가져 오기 기능입니다

import { Component } from '@angular/core'; 

export class Character { 
    characterId: number; 
    name: string; 
    description: string; 
    status: number; 
    userId: string; 
    companyId: number; 
} 

Company.ts에게 있습니다

import { Component } from '@angular/core'; 
import { Character } from './Character'; 

export class Company { 
    companyId: number; 
    name: string; 
    description: number; 
    characters: Character[]; 
} 

그리고 Character.ts

마지막으로 th 는 서비스 기능입니다

getCompany(id: number): Observable<Company> { 
     return this.http.get(this.url + "/Get/" + id, 
      { headers: this.authService.authJsonHeaders() }) 
      .map((resp: Response) => resp.json() as Company) 
      .catch(this.handleError); 
    } 

두 항목을 별도로 가져 오는 경우 두 모델이 모두 작동하지만 회사 내부의 문자를 반환하면 오류가 발생합니다.

크롬 디버거는이 오류를 보여줍니다

자원을로드하지 못했습니다 : 순 :: ERR_CONNECTION_RESET

그리고 구성 요소 로그이 오류

0 - { "isTrusted을"참 }

나는 무엇을보고 싶니? 응?

편집 : authHeader 통화 좋아

authJsonHeaders() { 
     let header = new Headers(); 
     header.append('Content-Type', 'application/json'); 
     header.append('Accept', 'application/json'); 
     header.append('Authorization', 'Bearer ' + 
      sessionStorage.getItem('bearer_token')); 
    return header; 
} 
+0

또한 'this.authService.authJsonHeaders()'가 반환하는 내용은 무엇입니까? – anoop

+0

"회사 내부의 문자를 반환하는 경우 오류가 발생합니다."이 작업을 수행하는 방법을 추가 할 수 있습니까? – echonax

+0

내 API 엔드 포인트에서 회사에 문자 데이터를 첨부 할 수 있습니다. 문자 데이터를 그대로두면 예상대로 응답이 회사 모델에 바인딩됩니다. 문자 데이터를 첨부하면 내가 게시하는 오류가 발생합니다. –

답변

1

,

에 JWT를 삽입되는 것은 분명히 문제는 시리얼 라이저했다. 어쨌든 아이들을 직렬화하는 것으로부터 오류가 발생했지만 유효한 json을 반환 할 수있었습니다.

순환 참조를 적절하게 방지하기 위해 더 많은 연결 요소에 [JsonIgnore] 태그를 던져 오류가 사라졌습니다.