2

I'm trying to read a json file from within my Angular2 src/app folder

The directory structure is as follows:-

enter image description here

And I'm trying to read the file in app.config.js using the following code:-

public load() {
    return new Promise((resolve, reject) => {
        this._http.get("config.json")
            .map(res => res.json())
            .subscribe((data) => {
                this.cache['config.json'] = new ConfigData(data.json());
                resolve(true);
            });
    });
}

But everything I try results in the following error:-

zone.js:2224 GET http://localhost:4200/config.json 404 (Not Found)

Where do I need to put the file?

I've tried moving it to the src directory to no avail. I've also tried:-

app/config.json ./app/config.json

I also added an assets folder under app and added it into there but that didn't work either

What do I need to do?

Thanks

7
  • You need to put external files in your assets folder. Commented Jul 4, 2017 at 15:47
  • I tried that but that also didn't work Commented Jul 4, 2017 at 15:47
  • Can you also add it to your question? Commented Jul 4, 2017 at 15:48
  • Added to the question Commented Jul 4, 2017 at 15:52
  • But how did you try to reach it, I mean what did you write to the url in your http request? Commented Jul 4, 2017 at 15:53

1 Answer 1

5

Put the config.json file in assets folder and change the request to following,

public load() {
    return new Promise((resolve, reject) => {
        this._http.get("assets/config.json")
            .map(res => res.json())
            .subscribe((data) => {
                this.cache['config.json'] = new ConfigData(data.json());
                resolve(true);
            });
    });
}

It should work. make sure to relunch the to make the changes reflect in www/assets folder.

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

3 Comments

Still gives the same error:- zone.js:2224 GET localhost:4200/assets/config.json 404 (Not Found)
you have to add the config.json file inside src/assets folder and not inside src/app/assets folder as you have mensioned in the question
@BigBytes If this solved the problem, then please mark the answer as correct!

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.