Cors Error Access-control-allow-origin in my mean app

Viewed 50

I have added headers middleware for nodejs server. I am got the following error before adding middlewares.Access to XMLHttpRequest at 'http://localhost:8000/machines' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

After correcting the error I am getting the following Access to XMLHttpRequest at 'http://localhost:8000/machines' from origin 'http://localhost:4200' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.

I have added the following code

app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
  res.header("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type");//Access-Control-Allow-Credentials
  // res.header("Access-Control-Allow-Credentials", "true");
  res.header('Content-Type', 'application/json');
  
  next(); // Important
})

After adding authorization to the Access-Control-Allow-Headers

I got this error again No 'Access-Control-Allow-Origin' header is present on the requested resource.

Can somebody help?

0 Answers
Related