-1

Hi I try to get data from https://swapi.dev/api/ but i recive error in console: 'Access to fetch at 'https://swapi.dev/api/people' from origin 'http://127.0.0.1:5500' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.'

I'm doing this in that way:

  fetch('https://swapi.dev/api/people',{
   headers: {
     "Access-Control-Allow-Origin": '*'
   }
  })
  .then(res=>console.log(res))

I noticed, that on incognito everything is working fine

AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
robert
  • 41
  • 6

1 Answers1

0

Just remove the options you have and it should work

remove the

  {
   headers: {
     "Access-Control-Allow-Origin": '*'
   }
  }

What the server replies is that they do not allow this header in the request.


Additionally, if you want to log the actual data you need to first extract it from the response object

fetch('https://swapi.dev/api/people')
  .then(response=>response.json())
  .then(data=>console.log(data))
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317