-1

I am trying to send post data using this code

  axios
.post(
  data.submissionUrl,
  { data: sendData },
  { headers: { "Access-Control-Allow-Origin": "*" } }
)
.then((response) => {
  console.log(response);
})
.catch((err) => {
  console.log(err, err.response);
});

and I am getting this error

Access to XMLHttpRequest at 'https://website' from origin 'vscode live server' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.

however I don't know for what reason if I swap data and headers places the headers are joined with data and the post goes through but the data is incorrect. data: "{"headers":{"Access-Control-Allow-Origin"

V.Cunichin
  • 27
  • 10

2 Answers2

-1

You cant do that. You need to add that header in the server response.

Clem
  • 2,150
  • 15
  • 17
  • so i have to add that header in the server (where I'm trying to post the data?) – V.Cunichin Apr 11 '21 at 20:18
  • yes, most server frameworks have some middlewares to do that. For example, for express: https://expressjs.com/en/resources/middleware/cors.html – Clem Apr 12 '21 at 05:51
  • You can use a proxy too. It s a common way get rid of cors. If you are serving your front app with nginx, you can setup a reverse proxy to make a redirection of '/api' to website. Your app and /api will be on the same host so the browser wont care about cors. – Clem Apr 12 '21 at 05:55
-1
  • i think that you used axios on Browser because if you used it with nodejs you won't get any problems with cors i suggest you to do it from your server by nodejs and you can get the data by http request for your server what i mean is
  1. create a script that sending the data from your server by nodejs
  2. create your browser script that sending a request to your server to get this data it's will be better if you using that in a website
Root Zxcvpn
  • 35
  • 1
  • 6