I deploy website that both use one domain but backend is sub domian something like api.domain.com
when I try to call api from frontend it notice me a simple cors issue or error
Access to XMLHttpRequest at 'https://api.example.com/api/v1/path/path' from origin 'https://www.example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
First time, I think I forgot to set cors origin but when I check it set properly
const corsOptions = {
origin: "https://www.example.com",
credentials: true,
};
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'https://www.example.com');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', "true");
// Pass to next layer of middleware
next();
});
Then I use cors debugger:
access-control-allow-credentials: true
access-control-allow-origin: https://example.vercel.app
It show my origin is my old domain I use vercel to host frontend then change to custom domain it seem origin not update when i set in cors option what I do wrong?
I’m trying to deploy website but cors origin not change properly
llxlust is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.