Another CORS problem, I’ve been dealing with issue for quite a bit, and haven’t been able to fix it. I’ve tried practically every solution I could find, but to no avail.
So, what is the issue?
I’ve deployed a client, server, and a database to Rendre. They all work fine; I can use Postman to query data from my server, as well as set values and create tables on my database using PG Admin.
However, when I go to my client, I encounter this CORS problem. Below is an image of what I see in the console window when I visit my client site:
From what I understand, this is a simple CORS problem, my server is rejecting the request my client is making to it, because the server doesn’t have an “Access-Control-Allow-Origin” to allow the client. So i set the CORS params to allow for it, like so:
app.use(
cors({
origin: “clientURL”,
})
);
ClientUR: https://video-streaming-client.onrender.com/
I redeployed, and the exact same error shows, ok weird, even if I allow all origins by setting the origin to “*” I get the same thing.
Ok let me search the internet, the most common solution I found that worked for everyone, is to not use cors, but rather use res.header, like so:
app.use((req, res, next) => {
res.header(
“Access-Control-Allow-Origin”,
“clientURL”
);
res.header(“Access-Control-Allow-Methods”, “GET, POST, PUT, DELETE, OPTIONS”);
res.header(“Access-Control-Allow-Headers”, “Content-Type, Authorization”);
res.header(“Access-Control-Allow-Credentials”, true);
console.log(“Request received:”, req.method, req.url);
next();
});
This looks better, because it sets the access control allow origin explicitly, but to not avail, still the same issue. When I go to my network tab, this is what I find:
Request URL: clienturl/celebs
Request Method : GET
Status Code : 502 Bad Gateway
Referrer Policy : strict-origin-when-cross-origin
I found another solution on render, but that also didn’t work. I’m taking a wild guess and saying maybe stripe is causing the issue, but I don’t know.
I have other full stack apps on render and they work fine.
In my server Environment Variables, I have this set up:
Access-Control-Allow-Origin = clientURL
The Access-Control-Allow-Origin is set to my client URL, ChatGPT recommended this, but I don’t think it does anything.