0

I am getting CORS error on localhost during post request call. below is the code

 fetch('http://sandbox.mywebsite.com/api//product-list/', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Access-Control-Allow-Origin': '*',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    "category_id":1
  })
})

Same giving proper response when testing on https://inspector.swagger.io/builder . Any help will be appreciated enter image description here

kunal
  • 165
  • 2
  • 3
  • 10
  • Always try searching for the error message first. There's most likely an existing answer. Or dozens. –  Mar 05 '21 at 19:52
  • yes, thank you. actually, I saw that post and tried all the methods but still getting errors. – kunal Mar 05 '21 at 19:53
  • 1
    The header needs to be set on the *server*. If you could simply use `'Access-Control-Allow-Origin': '*',` in your fetch call to circumvent it, the whole thing became pretty pointless. –  Mar 05 '21 at 20:06
  • thank you for the update. I already used this in my code during API call – kunal Mar 05 '21 at 20:11
  • 1
    ? Again: you cannot do this from the client. If you want to fetch something from an API without CORS, you need to route the request through your own server. –  Mar 05 '21 at 20:12

0 Answers0