-1

iam just now learning the asynchronous javascript.I have doubt in fetch API in javascript.

fetch(`./student.json`)
  .then((response) => {
    console.log(`resolved , ${response}`);
  })
  .catch((error) => {
    console.log(`rejected , ${error}`);
  });

and my json file is here 'student.json'

[
    {
        "name":"dhinesh",
        "college":"AAMEC",
        "dept":"CSE"
    },
    {
        "name":"niranjan",
        "college":"AAMEC",
        "dept":"EEE"
    }
]

and my output is " resovle, [object Response]"

i don't see any response object iam expecting the output is like this enter image description here

  • You have to perform `await response.json()` or `await response.text()` to get that data structure you looking – bogdanoff Aug 22 '23 at 15:06
  • Any fetch tutorial will show how to do this correctly. You need to call `response.text()`. – Barmar Aug 22 '23 at 15:07
  • Log the object without trying to interpolate it into the middle of a string. – Quentin Aug 22 '23 at 15:07
  • See https://www.javascripttutorial.net/javascript-fetch-api/ – Barmar Aug 22 '23 at 15:08
  • @Quentin The response object doesn't contain much useful information. – Barmar Aug 22 '23 at 15:08
  • Please refer to https://stackoverflow.com/questions/48841097/receive-and-process-json-using-fetch-api-in-javascript – The KNVB Aug 22 '23 at 15:10
  • You get that response because you are converting an Object to a String! `console.log(\`resolved , ${response}\`);` should be `console.log('resolved', response);` – epascarello Aug 23 '23 at 13:56
  • Your edit claims that you "don't see any Response object" but your question also states that you **do** see *[object Response]*. – Quentin Aug 23 '23 at 14:56

0 Answers0