1

Introduction

I have a local file 'countryballs.json' which I want to load it on a html and parse the data into the index.html.

The way I want to do it, and the only one I know, it's by using Fetch(). Quite easy to use.

Code

async function getData()
        {
            await fetch('./countryballs.json')
            .then((response) => {
                return response.json();
            })
            .then((data) => {
                console.log(data);
            })
            .catch(function (error) {
                console.log(error);
            })
        }
        getData();

Code explanation

Fetch should be used in a async function as it returns a promise (loading files takes time)

await fetch() //Will wait until all has being loaded. The rest of the code is self-explanatory ES6 JS coding.

Problem

When working with external API's (for weather, finance and stuff) it just works perfectly, I can retrieve and parse the data and everything goes well. Even loading local CVS files. But not with my local JSON file.

ERROR

index.html:15 GET http://localhost:8080/countryballs.json 404 (Not Found)

XMLHttprequest log:

index.html:15 Fetch failed loading: GET "http://localhost:8080/countryballs.json".

Questions

  • Why it does not work with JSON files too?
  • What I am missing in the code?

Example .json

[
  {
    "country": [
      {
        "name": "Spain",
        "GDP": " $1234",
        "Regions": " Europe ",
        "Align": " Western "
      }
    ]
  }
]

People with similar problems but different solutions or it didn't worked for me

Further explanation to understand the problem

  • The folder where I am working looks like this:

    • node-modules

    • index.html

    • countryballs.json

  • I am using Node.js and Visual Studio Code

  • The index.html runs on a localhost:8080 using Chrome Browser

Possible solution that I considered

  • Using the File System to load it (but still want to try fetch())

Image gallery of the problem

(Chrome's console output)

Hope you help me find a solution

Edit:

As some of you requested I will provide what I have here and below the instructions to test.

GoogleDrive file

1. Get node.js
2. cd into the folder with the index.html and the rest
3. call for `node http-server`
4. get into the browser and connect to localhost:8080 
Jonalcaide
  • 560
  • 8
  • 21
  • 1
    Had the same problem a week ago. I think response.json() returns a promise, too. – Kai Lehmann Jun 17 '19 at 16:57
  • 1
    @KaiLehmann Yeah, you need the await thing. But it is already "awaiting" the response and inside the async function. – Jonalcaide Jun 17 '19 at 16:58
  • What are you using to serve your index.html? – Christian C. Salvadó Jun 17 '19 at 16:59
  • 1
    No, what you return to your .then() is actually a Promise. What says the console.log(data?)? – Kai Lehmann Jun 17 '19 at 17:00
  • 1
    @CMS I run `node http-server` on the folder then I connect using the browser into `localhost:8080` – Jonalcaide Jun 17 '19 at 17:01
  • 1
    @KaiLehmann Nothing, the fetch failed as you can see on the XMLHttpRequest log – Jonalcaide Jun 17 '19 at 17:02
  • 1
    Have you tried fetch ("./countryballs.json")? Works in my localhost – Kai Lehmann Jun 17 '19 at 17:03
  • @KaiLehmann Doesn't work for me :/ – Jonalcaide Jun 17 '19 at 17:05
  • 1
    @WhiteGlove - this is easy enough to produce in the [plunkr](http://next.plnkr.co/edit/?p=catalogue&preview) - it is impossible for us to recreate your issue. If you can create you issue in plunkr then we can help. – Randy Casburn Jun 17 '19 at 17:07
  • @RandyCasburn http://next.plnkr.co/edit/FYChBHmYBL8Hbp10 It does works! I know my code is right... it's just that GET error... which can't fetch the .json file – Jonalcaide Jun 17 '19 at 17:13
  • 3
    Your response is a simple 404. There are several possibilities: 1) Your server (node) is not set up properly to recognize `.json` as a MIME type or 2) Your path is simply wrong or 3) you have implemented a _catchall_ route on your server that preempts the sending of the file - and since there is no _route_ to that URL, your server thows a 404. (this is the most likely scenario). – Randy Casburn Jun 17 '19 at 17:20
  • @RandyCasburn Could you please provide a path to follow and solve this situation? – Jonalcaide Jun 17 '19 at 17:20
  • 1
    In the code your using `/scripts`, but in the image and the plnkr your not using a directory, which is it? First, debug why you cant find the file by simply visiting it in your browser, then add the fetch code when you know that sanity check is ok. – Lawrence Cherone Jun 17 '19 at 17:22
  • @LawrenceCherone Sorry! Good call! – Jonalcaide Jun 17 '19 at 17:24
  • The image is different that the error messages, which is now different than the question code after the edit. This isn't hard. Your path MUST match the location of the file. It is that easy. You are way over your head with this simple code. You need to find a resource (human or otherwise) to get some of these basic building blocks understood before you continue to get frustrated. – Randy Casburn Jun 17 '19 at 17:27
  • @RandyCasburn You are right, but when I changed the PATH to check if that was the problem I forgot to update the image. Sorry for the confusion. I will load in a .rar the whole thing so you can test it and tell me if you get the same error. – Jonalcaide Jun 17 '19 at 17:31

0 Answers0