0

I tried searching for answers in the already asked similar questions but failed to resolve the problem

I am new to Angular 4 and trying to make a simple Angular frontend for a hypothetical book store. There is a hard-coded dataset present in the app as books.json. But the problem is every time to try to run it I get the following error:

Error showing in the ChromeSo,  Console

And here's the screenshot of the project directory and the code.

enter image description here

Here is my book.service.ts code :

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { IBook } from './book';
import 'rxjs/add/operator/map';

@Injectable()
export class BookService {

  constructor(private _http: Http){}

  getBooks(): Observable<IBook[]> {
    return this._http
    .get("api/books/books.json")
    .map((response : Response) => {
      return response.json() as IBook[];
    });
  }
}
3
  • This has nothing to do with Angular. Resource api/books/books.json is simply not there. Check your path. Commented Dec 20, 2017 at 15:09
  • Don't we need to give the location with respect to the index.html file? Commented Dec 20, 2017 at 15:14
  • No! Most files are processed and moved to www/ folder. Contents of assets/ folder are simply copied over to www/ so this would be the best place to put your .json files. If you move your .json to assets/ you should then access it with assets/api/books/books.json. Commented Dec 20, 2017 at 15:22

3 Answers 3

3

One option is to move the json files to the src/assets folder. A better solution would be to edit angular-cli.json and add your folder (route) to apps.assets:

"assets": [
    "api",
    "assets",
    "favicon.ico"
]

Now the json files should be accessible, you don't need to change any code. Don't forget to stop serving the application and run ng serve again.

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

2 Comments

Tried doing that, but still getting the same error. Don't I need to give the location with respect to the index.html file?
My bad, don't move the api folder; keep it inside src. This should work as I got it working locally. Don't forget to stop serving the application and run ng serve again.
2

move your api folder under root folder. In this case under book-store.

The issue was not in the code. its about finding the path to the json file is invalid

Or

you can keep the files under src folder and add the api into assets in angular-cli.json

 "assets": [
    "assets",
    "favicon.ico",
    "api"
 ],

2 Comments

Or better yet. Move it to src/assets/ as this seems to be an asset.
I tried doing that. But still getting the same error.
1

Your folder structure seems ok. The problem is that your server does not know anything about api folder. In your .angular-cli.json, put "api" to your assets array like following

 ...
 "assets": [
    "assets",
    "favicon.ico",
    "api"
 ],
 ...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.