Angular 6: HttpClient Get JSON File 404 Error

后端 未结 3 826
你的背包
你的背包 2020-12-21 07:32

Using the HTTP Client, I\'m to retrieve a JSON file which resides the assets directory within my Angular 6 App which was generated using the CLI. While I know t

3条回答
  •  臣服心动
    2020-12-21 08:02

    Several notes...

    After running the server, just do a simple thing: copy/paste your resource URL into the browser, like this:

    If you see the JSON then everything is fine, it means the problem is with how it is requesting from the Angular app. To understand the issue open Network section (or console) in your browser, most probably the issue is that how you are requesting the file.

    For example, this: this.http.get('http://localhost:4200/assets/us-all-all.geo.json');
    will most probably be converted to:
    http://localhost:4200/http://localhost:4200/assets/us-all-all.geo.json'.

    See my point?

    Solution is simply to call the resource without http://localshost:4200,
    like:
    this.http.get('assets/us-all-all.geo.json');

提交回复
热议问题