2

I am trying to load a .json file from a service file in angular2. I install my angular2 app using angular-cli. My app file structure is like below

src > app > menu-item > menu-item.service.ts

menu-list.json

Here is my menu-item.service.ts file.

getMenuListObservable(): Observable<Menu[]> {
    return this._http.get('menu-list.json')
        .map((response: Response) => <Menu[]>response.json().menuList)
        .do(data => console.log(JSON.stringify(data)))
        .catch(this.handleError);
     }

http://localhost:4200/menu-list.json [HTTP/1.1 404 Not Found 173ms] Object { _body: "Cannot GET /menu-list.json ", status: 404, ok: false, statusText: "Not Found", headers: Object, type: 2, url: "http://localhost:4200/menu-list.json" }

If I place then menu-list.json file in the root directory then its work, but I don't want to place it on root directory. If I change the path name like that return this._http.get('src/app/menu-item/menu-list.json') its also give me same error msg.

Please give me a suggestion what I have to do for that.

3
  • 1
    Have you tried return this._http.get('app/menu-item/menu-list.json') ? Check this plunker for a similar example: plnkr.co/edit/60E2qb9gOjvkEAeR5CtE?p=info Commented Feb 17, 2017 at 16:52
  • @echonax would it work if she's using routing ? Commented Feb 17, 2017 at 17:02
  • @echonax, yes you are absolutely right, its working. Thanks a lot. Commented Feb 17, 2017 at 17:05

1 Answer 1

2

It should be return this._http.get('app/menu-item/menu-list.json') Check this plunker for a similar example: http://plnkr.co/edit/60E2qb9gOjvkEAeR5CtE?p=info

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.