[SOLVED] ERR_CONNECTION_REFUSED in Angular and Spring

net::ERR_CONNECTION_REFUSED : I’d like to access an API server, but the server hasn’t started. Then I got the error:

http://localhost:3000/api/heroes net::ERR_CONNECTION_REFUSED

Could I handle this according to the status of the response ?

ERR CONNECTION REFUSED indicates that the connection has not been established (the request has not been completed), hence the status code is 0.

Solution 1:

//First inject the router in the constructor

private handleError (error: Response | any) {

//Your other codes    
  if (error.status == 0) { //or whatever condition you like to put
     this.router.navigate(['/error']);
  }
}

Solution 2 :

If you are using spring for the backend, just add the annotation @crossorigin in your rest controller to allow your front to consume the API.

@RestController
@CrossOrigin // you can add (origins = "http://localhost:4200") to set only one source

Find more Angular Bug solution on jTuto.com

people found this article helpful. What about you?