0

I have an application built on angular and node js and when after login into application, there is an error on fetching data as it is shown me CORS error? how should I resolve this error?

enter image description here

Sebastian Speitel
  • 7,166
  • 2
  • 19
  • 38

2 Answers2

0

The problem is probably caused by the API running on a different port. See CORS error on same domain?

Try adding Access-Control-Allow-Origin and Access-Control-Allow-Methods headers to your API.

This is how this is achieved on Express using middleware.

The following allows all origins and methods (this might be too open depending on your requirements).

const express = require('express');
const app = express();
const port = 3000;

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', '*');
  next();
});

app.get('/', (req, res) => res.send('Hello World!'));

app.listen(port, () => console.log(`Example app listening on port ${port}!`));
sunknudsen
  • 6,356
  • 3
  • 39
  • 76
0

This error is due to app running on port 1949 rejecting the request from port 80, bcos it considers as cross origin request. You need to config CORS for the app on port 1949 to access from any origin origin: "*", sample for node js:

server.use(
  cors({
    origin: "*",
    optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
  })
);
Vengleab SO
  • 716
  • 4
  • 11