-3

I am getting the following error when sending a POST api request to my express server.

Access to XMLHttpRequest at 'localhost:8081/application' from origin 'localhost:8083' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

The express server is using the npm cors package and passport jwt authentication strategy.

privateServer.use(cors());
registerJWTAuthentication();
  privateServer.all('*', authenticate('jwt', {
    session: false,
  }));
privateServer.addRouter('/application', privateApplicationRouter);

I am sending this request with axios from a nuxtjs application.

const result = await axios.post('localhost:8081/application', payload);

Does anyone know how to fix this CORS problem?

Leahpar
  • 583
  • 1
  • 4
  • 12
  • I’ve suggested an edit to the question title - the answer to “how to handle CORS problems” would be along the lines of “weep into a deep dark well”, or “burn it all and walk away” – Tom Harvey Jul 04 '20 at 07:47
  • refer https://stackoverflow.com/questions/33483675/getting-express-server-to-accept-cors-request – Madusudhanan Jul 04 '20 at 07:54
  • do not work. I have a public server too (withour authentication) there it works withour any changes. – Leahpar Jul 04 '20 at 08:07
  • Try adding 'Access-Control-Allow-Origin': '*' to your axios request on the frontend. Ususally cors enables all origins when running on your local machine. I had issues even with cors once i deployed it to a serverless environment and other server stuff. Maybe that helps :/ – Jan-Philipp Marks Jul 04 '20 at 08:52

1 Answers1

-1

the cors middleware allows you to pass options, you can do something like :

  {
      "origin": "localhost:8083",
      "credentials": true,
  }

Note that 'origin' can accept an array of origins you are allowing, and your request should also contain :

 {
     credentials: 'include',
 }

In order for the cookies to be sent,

Hope it helps ...

jinwar
  • 366
  • 2
  • 8