net::ERR_CONNECTION_REFUSED with http post in angular2

Viewed 7509

I am getting the following error with POST request:

POST http://127.0.0.1:port/v1/route2 net::ERR_CONNECTION_REFUSED

GET request is working perfectly fine. What could be the possible reason for the failure of POST? Is there any mistake in the testPost?
Here is the part of angular service:

@Injectable()
export class SampleService {
    private _getUrl: string = 'http://<server-ip>/v1/route1';
    private _postUrl: string = 'http://127.0.0.1:<port>/v1/route2';
    constructor(private _http: Http) {}
    testGet() {
        return this._http.get(this._getUrl).map((response: Response) => response.json());
}
    testPost() {
        const headers = new Headers();
        headers.append('Content-Type', 'application/json; charset=utf-8');
        this._http.post(this._postUrl, JSON.stringify({'key1': 'value1', 'key2': 'value2'}), headers).subscribe(() => {}, err => console.error(err));
    }
}
3 Answers

Well, there is no port = port. You need to define a real port. By default it is 80 and your application is maybe running using this port, thats why get is working. But your explicit definition of the port in the second call is the issue.

Connection Refused means there is not real application handling you request. If the header would be wrong i guess the application itself would throw an (application) error

There might be some problems which can cause these kind of errors

  • Remove the port number after the IP address if your system don't need it.
  • If you are communicating with PHP or other technologies, you should change the permission in .htacces file for the angular project.
Related