R
Railway14mo ago
Sankalan

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
Percy14mo ago
Project ID: e4cfbada-ee8d-4956-b396-6eebd2672653
Sankalan
Sankalan14mo ago
project ID : e4cfbada-ee8d-4956-b396-6eebd2672653
Brody
Brody14mo ago
properly apply the cors settings
Sankalan
Sankalan14mo ago
I did, the backend is working fine locally with CrossOrigin enabled.
Brody
Brody14mo ago
what status code do you get when accessing the url from the browser
Sankalan
Sankalan14mo ago
No status code, I get the message "Access-Control-Allow-Origin" blocked by "https://newsflash-backend-production.up.railway.app" in console
Brody
Brody14mo ago
please tell me the status code
Sankalan
Sankalan14mo ago
I didn't get any status code, the deployed project is working fine from postman api testing.
Finn
Finn14mo ago
i suppose there wont be any as the request is blocked by the browser
Sankalan
Sankalan14mo ago
it was 200 ok
Finn
Finn14mo ago
in anycase you need to set cors headers correctly probbaly the preflight request yes
Brody
Brody14mo 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
Sankalan14mo 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
Brody14mo ago
as we have both said: you need to set cors headers correctly
Sankalan
Sankalan14mo 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
Brody14mo ago
it would be a code issue, not an issue with the platform
Sankalan
Sankalan14mo 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
Brody14mo ago
i will mark as solved
Sankalan
Sankalan14mo ago
Thanks. It was very silly me!
Brody
Brody14mo ago
no problem