-1

``I am new with API requests, I have to use an API with authentication but I have an error. I have tried to solve the cors but I have not found the solution. When I put mode no-cors I have error 401. The API is external, I haven't access to modify anything

fetch("https://xxxxxxx",{
    method:"GET",
    headers: {
      'Content-type' : 'application/json',
      'Access-Control-Allow-Origin': '*',
      "X-AMC-Vendor-Key": "xxxxxxxxx"
    }
  })
  .then(res => res.json())
  .then(
    (result) => {
      console.log('rsult 1', result)
    },
    
    (error) => {
      // handle error
      console.log('error', error)

    }
  )
  • Does this answer your question? [CORS error even after setting Access-Control-Allow-Origin or other Access-Control-Allow-\* headers on client side](https://stackoverflow.com/questions/44232370/cors-error-even-after-setting-access-control-allow-origin-or-other-access-contro) – jub0bs Sep 28 '22 at 18:23

1 Answers1

-1

Server also needs to allow cors, or add allow cors to response object.

Access-Control-Allow-Origin : //set this field as the client domain or * Access-Control-Allow-Origin : http://example.client.come // this will give example.client.com as a client to bypass cors by browser

or

Access-Control-Allow-Origin: * // this will allow any client server to access the api on server domain

Megha Bisht
  • 74
  • 1
  • 3