简体   繁体   中英

GET http://localhost:4200/api/x.json 404 (Not Found) - angular 2



Hello, There is a problem with a project that does not recognize a json file - and I do not know why. Is there anything I need to change or make it work?


this is my folders: 在此处输入图片说明


this is my service:

import { Injectable } from "@angular/core";
import { Ibrides } from "./brides";
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';

@Injectable()
export class brideService {
private _brideUrl = 'api/brides.json';
constructor(private _http: HttpClient) { };

getBrides(): Observable<Ibrides[]> {

    return this._http.get<Ibrides[]>(this._brideUrl)
        .do(data => console.log('All:' + JSON.stringify(data)))
        .catch(this.handleError)
}
private handleError(err: HttpErrorResponse) {
    console.log(err.message);
    return Observable.throw(err.message);
}
}

this is my component


import { Component, OnInit } from '@angular/core';
import { Ibrides } from "./brides";
import { brideService } from "./brides.service"

@Component({
selector: 'pm-brides',
templateUrl: './brides_list.component.html',
styleUrls: []
})
export class bridesListComponent implements OnInit {


    constructor(private _brideService: brideService) {

    }
    errorMessage: string;
    brides: Ibrides[] = [];
    ngOnInit(): void {
    this._brideService.getBrides()
        .subscribe(brides => {
            this.brides = brides
        },

        error => this.errorMessage = <any>error);

    }

}

Just reference the file from the root level like this:

_brideUrl = 'app/api/brides.json'

For more information you can refer to this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM