CORS Error

I am encountering CORS error from react frontend after I deployed my Spring boot backend to Railway, although the same code is working from default spring boot route "localhost:8080". The deployed backend is working fine during API testing using Postman. How to solve that?
20 Replies
Percy
Percy2y ago
Project ID: e4cfbada-ee8d-4956-b396-6eebd2672653
Sankalan
SankalanOP2y ago
project ID : e4cfbada-ee8d-4956-b396-6eebd2672653
Brody
Brody2y ago
properly apply the cors settings
Sankalan
SankalanOP2y ago
I did, the backend is working fine locally with CrossOrigin enabled.
Brody
Brody2y ago
what status code do you get when accessing the url from the browser
Sankalan
SankalanOP2y ago
No status code, I get the message "Access-Control-Allow-Origin" blocked by "https://newsflash-backend-production.up.railway.app" in console
Brody
Brody2y ago
please tell me the status code
Sankalan
SankalanOP2y ago
I didn't get any status code, the deployed project is working fine from postman api testing.
Finn
Finn2y ago
i suppose there wont be any as the request is blocked by the browser
Sankalan
SankalanOP2y ago
it was 200 ok
Finn
Finn2y ago
in anycase you need to set cors headers correctly probbaly the preflight request yes
Brody
Brody2y ago
well what i was getting at is that maybe it was returning 404, and the cors headers wouldn't be present on a 404 response, so cors might not actaully be the issue even if it does return 404, browsers will still throw a cors error, even though the 404 is the real issue but i see that you just said it returned a 200 code, then yes you need to set your cors headers correctly
Sankalan
SankalanOP2y ago
But it shouldn't. In the backend I have used Spring Security, so it may return 403 unauthorized on undefined endpoints(which it is), but the defined ones are working correctly through API testing, but once I try to connect from my React frontend, it isn't working and throwing the CORS error.
Brody
Brody2y ago
as we have both said: you need to set cors headers correctly
Sankalan
SankalanOP2y ago
The same CORS headers seem to be working on localhost. But I will check again and get back to you. Is there any possible reason why it works locally but not after deployment?
Brody
Brody2y ago
it would be a code issue, not an issue with the platform
Sankalan
SankalanOP2y ago
Thanks a lot, both of you. I will look into that. I have solved the issue. I missed to put "/api/v1" in the deployed backend URL as defined in my application. How do I close this issue?
Brody
Brody2y ago
i will mark as solved
Sankalan
SankalanOP2y ago
Thanks. It was very silly me!
Brody
Brody2y ago
no problem
Want results from more Discord servers?
Add your server