I'm trying to use axios to make a post request to my backend. Every time I make the request I was receiving this error:
origin 'http://localhost:1234' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
So I added the appropriate header:
const FRONTEND_URL = 'http://localhost:1234'
export async function connect( address:string | undefined, signature:string | undefined ) {
const url = `${SERVER_URL}/connect`;
return await axios.post(
url,
{ address, signature },
{
headers: {
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Origin': `${FRONTEND_URL}`,
'content-type': 'application/json',
},
}
);
}
I still get the same error. Why isn't it detecting my Access-Control-Allow-Origin header? (I've restarted servers multiple times)
Since you are running the routes in development at it means using various ports you are running into error due to CORS policy. You may look up to these answers as they are provided already with so much explanation enable cors.
Also, you may look at documentation of CORS package CORS