Unable to read the data from the external JSON file


#1

I have configured all the URL’s in a JSON file called ionic-config.JSON . The data is like

[
    {
        "db_url": "http://localhost:3000/users/"
    }
]

I’m the trying to read the data using the GET method.

url: string = '../ionic-config/ionic-config.json';
this.http.get(this.url).subscribe( (data: any) => {
      this.db_url = data;
    })

But I’m getting an error as 404 no found

GET http://localhost:8100/ionic-config/ionic-config.json 404 (Not Found)

The file is in the directory src/ionic-config/ionic-config.json


#2

Hi,

I have this code:

protected _readBaseFile(file: string)  : Observable<string>{
        return Observable.create((observer) => {
            console.warn("READING FILE FROM BASE FOLDER", file);
            this._http.get<Array<any>>('assets/base/DB/' + file)
            .subscribe(
                (text: Array<any>) => {
                    observer.next(JSON.stringify(text));
                    observer.complete();
                },
                (err) => {
                    console.error("BD-FROM-JSON : Failed to load file", err);
                    observer.error(err)
                }
            )
        });
    }

with this I open a file from the assets folder and works fine.


#3

Why not just:

_readBaseFile(file: string): Observable<string> {
  return this._http.get('assets/base/DB/' + file, {responseType: 'text'});
}

?


#4

Yes, I need to do a lot of refactoring, but the application is in production now. Not touch it!


#5

Can I import the json file using the keyword import in the .ts file? If yes, how ?