2017-09-14 12 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 
    }] 
} 

ここでは、これはコンポーネント

の私のGet関数であるCompany.ts

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

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

そしてCharacter.ts

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

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

です

getCompany(id: number) { 
     this.companyService.getCompany(id) 
      .subscribe(
      (data: Company) => this.company = data, 
      error => this.errorMessage = <any>error); 
    } 

最後に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を":真}

ng?

編集: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エンドポイントでは、会社に文字データを添付することもできません。文字データをそのままにしておくと、応答は期待どおりにCompanyモデルにバインドされます。私が文字データを添付すると、私が投稿しているエラーが発生します。 –

答えて

1

オーケーにJWTを挿入され 、

はどうやら問題は、シリアライザにありました。それは子供を連載してエラーが出ましたが、とにかく有効なjsonを返すことができました。

循環参照を適切に防止するために、より多くの接続要素に[JsonIgnore]タグを入れてエラーが消えました。

関連する問題