0

there was a problem with the fetch request to the express server via vue cli, there was an error with "cors", fixed it, now I get the request, but I see it in the networke of the developer tool enter image description here but the console is empty enter image description here here is the code:

methods: {
async getInfo () {
  const response = await fetch('https://ksjkfsjdbnfksjfksjf744.netlify.app/.netlify/functions/api/demo',
    { mode: 'no-cors' })
  const responseString = await response
  const responseObject = await JSON.parse(JSON.stringify(responseString))
  console.log(responseString)
  console.log(responseObject)
}

can't solve the problem

1 Answers1

0

Fetch documentation

The Response object, in turn, does not directly contain the actual JSON response body but is instead a representation of the entire HTTP response. So, to extract the JSON body content from the Response object, we use the json() method, which returns a second promise that resolves with the result of parsing the response body text as JSON.

In summary, to get your JSON, use json() method on the returned fetch response

const response = await fetch('https://ksjkfsjdbnfksjfksjf744.netlify.app/.netlify/functions/api/demo',
    { mode: 'no-cors' })
const responseObject = await response.json()
console.log(responseObject)

Edit:

This will only work if mode: 'no-cors' is removed. Reasoning is here. Instead of trying to bypass CORS, the better solution is to add the frontend's origin to be allowed by CORS on the backend.

yoduh
  • 7,074
  • 2
  • 11
  • 21
  • json() did not help – Vatorio Jun 13 '23 at 17:53
  • I wasn't familiar with it before, but it looks like an issue with using `mode: 'no-cors'`. The response body is not **accessible to frontend javascript code** when using this mode (but still seeing it in the network tab is expected). See [this answer](https://stackoverflow.com/a/43319482/6225326) that goes into more detail. The ultimate solution will be to modify your backend API and add your frontend URL as an allowed CORS origin. – yoduh Jun 13 '23 at 18:36