json文件的Angular 2 http GET返回404

前端之家收集整理的这篇文章主要介绍了json文件的Angular 2 http GET返回404前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在做一个POC来证明我在Angular Universal的后端和前端之间有通信.我在后端有一个名为heroes.json的 JSON文件,我想从model.service.ts中的前端服务ModelService中检索.

我有这个文件夹结构:

在model.service.ts(前端)中,我想创建一个http请求,以获取一个名为getStuff()的方法中的一些数据.

我在model.service.ts中有这个:

  1. // domain/feature service
  2. @Injectable()
  3. export class ModelService {
  4. private heroesUrl = "http://localhost:4000/src/backend/heroes.json"; // URL to JSON file
  5. // This is only one example of one Model depending on your domain
  6. constructor(public api: ApiService,public cacheService: CacheService,private http: Http) {
  7.  
  8. }
  9.  
  10. public getStuff(): Observable<any[]> {
  11. return this.http.get(this.heroesUrl)
  12. .map(this.extractData)
  13. .catch(this.handleError);
  14. }
  15.  
  16. private extractData(res: Response) {
  17. let body = res.json();
  18. return body.data || { };
  19. }
  20.  
  21. private handleError (error: Response | any) {
  22. // In a real world app,we might use a remote logging infrastructure
  23. let errMsg: string;
  24. if (error instanceof Response) {
  25. const body = error.json() || "";
  26. const err = body.error || JSON.stringify(body);
  27. errMsg = `${error.status} - ${error.statusText || ""} ${err}`;
  28. } else {
  29. errMsg = error.message ? error.message : error.toString();
  30. }
  31. console.error(errMsg);
  32. return Observable.throw(errMsg);
  33. }
  34.  
  35. // domain/feature service
  36. @Injectable()
  37. export class ModelService {
  38. private heroesUrl = "http://localhost:4000/src/backend/heroes.json"; // URL to JSON file
  39. // This is only one example of one Model depending on your domain
  40. constructor(public api: ApiService,private http: Http) {
  41.  
  42. }
  43.  
  44. public getStuff(): Observable<any[]> {
  45. return this.http.get(this.heroesUrl)
  46. .map(this.extractData)
  47. .catch(this.handleError);
  48. }
  49.  
  50. private extractData(res: Response) {
  51. let body = res.json();
  52. return body.data || { };
  53. }
  54.  
  55. private handleError (error: Response | any) {
  56. // In a real world app,we might use a remote logging infrastructure
  57. let errMsg: string;
  58. if (error instanceof Response) {
  59. const body = error.json() || "";
  60. const err = body.error || JSON.stringify(body);
  61. errMsg = `${error.status} - ${error.statusText || ""} ${err}`;
  62. } else {
  63. errMsg = error.message ? error.message : error.toString();
  64. }
  65. console.error(errMsg);
  66. return Observable.throw(errMsg);
  67. }

从前端组件我调用ModelService.getHeroes:

  1. export class HomeComponent {
  2.  
  3. public data: any = {};
  4. constructor(public modelService: ModelService) {
  5. // we need the data synchronously for the client to set the server response
  6. // we create another method so we have more control for testing
  7. this.universalInit();
  8. }
  9.  
  10. public universalInit() {
  11.  
  12. this.modelService.getStuff().subscribe((data) => {
  13. this.data = data;
  14. });
  15. }

我收到这个错误

  1. GET /src/backend/heroes.json 404 3.698 ms - 46
  2. 404 - {"status":404,"message":"No Content"}
  3. EXCEPTION: 404 - {"status":404,"message":"No Content"}
  4.  
  5. /private/var/root/vepo/node_modules/rxjs/Subscriber.js:227
  6. throw err;
  7. ^
  8. 404 - {"status":404,"message":"No Content"}
  9. [nodemon] app crashed - waiting for file changes before starting...

所以我的url私人heroesUrl =“http:// localhost:4000 / src / backend / heroes.json”; //服务中JSON文件的URL错误.鉴于文件夹结构,网址是什么?因为实际运行的项目,输出,是在dist:

所以我不确定将什么放入ModelService.heroesUrl. ModelService.heroesUrl应该有什么字符串值?

解决方法

你必须将你的json文件放入你的dist文件夹客户端,你必须将你的URL更改为http:// localhost:4000 / dist / heroes.json< - 将你的json文件放在dist目录中的目标

猜你在找的JavaScript相关文章