1

I am just learning Vue js and are using it in the simplest possible way by including the source code in the end of the html. Now I am trying to do an ordinary JavaScript fetch like this:

fetch('./json_docs/example.json')
   .then(function(response) {
      return response;
   })
   .then(function(res) {
      console.log(res);
  });

The response I get looks like this but I don't get the actual data back. When I try using the URL to the file that's included in the response in the browser the data shows. Does anyone know what I am doing wrong?

Response { type: "basic", url: "file:///Users/danielamir/Documents/…", redirected: false, status: 200, ok: true, statusText: "OK", headers: Headers, bodyUsed: false }

5
  • 3
    Call .json on the response. See Making Fetch Requests Commented Mar 5, 2018 at 17:35
  • Doesn't seem to work, it just returns 'function json()' when I log response.json @zero298 Commented Mar 5, 2018 at 17:37
  • Call it with .json() using parentheses. See my answer. Commented Mar 5, 2018 at 17:38
  • Also, seeing file:// in the URL of the response worries me. How are you serving this content? Does the URL in your address bar have file:// in it? Commented Mar 5, 2018 at 17:41
  • I am getting a file just as a test, so that's why @zero298 Commented Mar 5, 2018 at 17:43

1 Answer 1

2

You need to actually call .json on the response:

fetch('./json_docs/example.json')
   .then(function(response) {
      return response.json(); // As a function call
   })
   .then(function(data) {
      console.log(data);
  });

See Making Fetch Requests

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

Comments

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.