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