Evgheny Kalkutin Evgheny Kalkutin - 1 year ago 183
Node.js Question

Angular 2 service http post doesnt work

I have nodejs server listening & waiting for post requests


Server is tested with PostMan for JSON post request and all is working, nodejs outputs 'Got data' to console. But Angular2 doesnt seems to send data to server.Screenshot PostMan

Whats the problem?

NodeJS server.js:

api.post('/updateLocation', function(req, res){
console.log('Got data');

Ionic 2 home.ts:

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {Service} from '../../service';

templateUrl: 'build/pages/home/home.html',
providers: [Service]
export class HomePage {
constructor(private navCtrl: NavController, service: Service) {
setInterval( ()=> {
service.sendLocation({ x: '46.303344', y: '28.655268' });
}, 1000);



import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';

export class Service {
http : any;
url : string = 'http://localhost:3000/api/updateLocation';
constructor(http: Http){
this.http = http;

let body = JSON.stringify(location);
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post(this.url, body, { headers });

Answer Source

You need to subscribe to the http action, otherwise the request isn't actually fired. It remains 'cold' until it's used.

This would work, if your api was returning a json response, for example:

this.http.post(this.url, body, { headers }).subscribe(
        data => {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download