私はjsonファイルからデータを抽出して配列に格納しようとしています。現在、私はこれらのエラーを取得しています:角度json.data()as array []
zone.js:2933 GET http://localhost:4200/app/persons.json 404 (Not Found)
と
core.es5.js:1020 ERROR Error: Uncaught (in promise): undefined
at resolvePromise (zone.js:824)
at resolvePromise (zone.js:795)
at zone.js:873
これは私のサービスである:
import { Injectable } from '@angular/core';
import { Actor } from '../model/actor';
// import { ACTORS } from './mock-actors';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class ActorService {
private actorsUrl = '/app/persons.json';
constructor(private http: Http) { }
getActors(): Promise<Actor[]> {
return this.http.get(this.actorsUrl)
.toPromise().then(response => response.json().data as Actor[])
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('Error: ', error);
return Promise.reject(error.message);
}
}
私のモデル
export class Actor {
lastName: string;
profession: string;
bio: string;
url: string;
imageUri: string;
name: string;
id: number;
realName: string;
}
JSONファイルは次のようになりますこの
{"personList":[{"lastName":"Wolowitz","profession":"Aerospace Engineer","bio":"Howard Joel Wolowitz, M.Eng is a fictional character on the CBS television series The Big Bang Theory, portrayed by actor Simon Helberg. Among the main male characters in the show, Howard is distinguished for lacking a doctoral degree, for still living with his mother, and for believing himself to be a \"ladies' man\". Simon Helberg's character is named after a computer programmer known by the show's co-creator Bill Prady.","url":"https://en.wikipedia.org/wiki/Simon_Helberg","imageUri":"Howard_Wolowitz.jpg","name":"Howard","id":"howard","realName":"Simon Helberg"},{"lastName":"Hofstadter","profession":"Experimental physicist","bio":"Leonard Leakey Hofstadter, Ph.D., is a fictional character on the CBS television series The Big Bang Theory, portrayed by actor Johnny Galecki. Leonard is an experimental physicist originally from New Jersey who shares an apartment with colleague and friend Dr. Sheldon Cooper (Jim Parsons). Leonard and Sheldon are named after actor/producer Sheldon Leonard, and Nobel Prize Laureates Robert Hofstadter and Leon Cooper.\nLeonard has been described as the straight man of the series. Penny (Kaley Cuoco) is Leonard's next-door neighbor and main love interest, and the teasing of romance between the two of them is a major force driving the series. For his portrayal, Galecki was nominated for a Primetime Emmy Award and a Golden Globe Award.","url":"http://en.wikipedia.org/wiki/Johnny_Galecki","imageUri":"Leonard_Hofstadter.jpg","name":"Leonard","id":"KLJSMLND","realName":"Johnny Galecki"}}
EDIT
私はこの
{
"/app/*":{
"target": "appfoundry-restdemo.herokuapp.com/";, "secure": false, "logLevel": "debug"
}
}
パスは現在のtsファイルからの相対パスである必要があります。あなたのパスを確認してください – Korte
アクセスしているjsonファイルの相対パスを使ってみてください。 –
jsonファイルに接続するためにプロキシを使用する必要があります。偽、 "ログレベル": { "/アプリ/ *":{ "ターゲット": "http://appfoundry-restdemo.herokuapp.com/"、 は "安全な" これは、プロキシファイルであります: "debug" } } – Gurbii