I have a frontend in Angular that access a backend in Java Spring.
Both in my dev computer work well and I can access and run the application in the browser correctly.
Now I need to deploy it to a test server.
I'm deploying both frontend and backend to the same test server in Tomcat \webapps.
In the test server both backend and frontend run on port 8080.
The problem is that when I access the deployed application from my dev machine like this:
http://testserverip:8080/myapp
I'm getting this error in Chrome Dev Tools:
GET http://localhost:8080/api net::ERR_CONNECTION_REFUSED
UPDATE:
I originally thought this was a CORS issue.
But realized it is not because both frontend and backend are running in the same Origin (http://localhost:8080) as both were deployed to the same Test Server, in the same Tomcat installation \webapps and in the same port.
So I'm removing the CORS code from the question.
Now I placed logs in the frontend caller method and in the backend controller method.
It does execute correctly the frontend (the frontend log gets written).
Then it calls the backend controller method.
But backend controller log never gets written.
So it is not getting to the backend.
The issue only happens when accessing the application frontend from the dev machine web browser.
If I log into the test server, open a browser there and access the frontend that way, it executes both frontend and backend correctly.
So what else could be blocking backend to be executed when accessing the front end outside the test server?
Like mentioned in the above comments, after a lot of time on this I found the issue.
The angular code was calling the rest api this way:
http://localhost:8080/api
When I changed it to the actual server ip address it worked perfectly.
http://xx.xx.xxx.xx:8080/api