Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Error i got

Access to XMLHttpRequest at 'http://localhost:3001' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value 'http://localhost:3001/' that is not equal to the supplied origin.


What I have tried:

i created a backend for this
my backend file index.js

JavaScript
const corsOptions = {
    origin: 'http://localhost:3001/',
    optionsSuccessStatus: 200,
    credentials: true
    
}

app.use(cors(corsOptions));


app.use((req,res,next)=>{
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    next();
})

const PORT = process.env.PORT // 3001


can anyone tell why I am getting error
Posted
Updated 19-Sep-22 21:40pm

1 solution

Quote:
origin: 'http://localhost:3001/'
Only http://localhost:3001/ is allowed access.
Quote:
... from origin 'http://localhost:3000' ...
The request is blocked because 3000 is not the same as 3001.

Cross-Origin Resource Sharing (CORS) - HTTP | MDN[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900