Access to XMLHttpRequest at '...' from origin 'localhost:3000' has been blocked by CORS policy Access to XMLHttpRequest at '...' from origin 'localhost:3000' has been blocked by CORS policy reactjs reactjs

Access to XMLHttpRequest at '...' from origin 'localhost:3000' has been blocked by CORS policy


if you are building your rest api in nodejs. Follow the folowing simple steps

Stop the Node.js server.

npm install cors --save

Add following lines to your server.js or index.js

var cors = require('cors')app.use(cors()) // Use this after the variable declaration

Now try to make your api call on the client side and it should work


After many days finally I got a solution . Instead of using CORS simply like this

const cors = require('cors');

app.use(cors());

in your server index.js using CORS option will solve the issue and now you can pass cookies or other credentials

const cors = require('cors');const corsOptions ={    origin:'http://localhost:3000',     credentials:true,            //access-control-allow-credentials:true    optionSuccessStatus:200}app.use(cors(corsOptions));