2

Trying to make a angular2 .post( )/.put( ) call, but nothing comes in backend. No errors, nothing. Seems the call just die

import 'rxjs/add/operator/map';
import {Http, Headers, RequestOptions} from '@angular/http';

let body = JSON.stringify(this.user);
    let headers = new Headers({
        'Content-Type': 'application/json'
    });
    let options = new RequestOptions({ headers: headers });

    return new Promise( ( resolve ) => {
        this.http.post( this.LOGIN_URL, body, options )
            .map( ( res ) => res.json() )
            .subscribe(
                ( data ) => resolve(data),
                ( error ) => resolve(error)
            );
        });

When i change to .get( ) / .delete( ) i receive the call on node backend, so, works fine:

this.http.get( this.LOGIN_URL ) 
this.http.delete( this.LOGIN_URL ) 

What is wrong with my .post( ) / .put( ) calls?

Lothav
  • 183
  • 7
  • Assuming nothing is null (e.g. `this.user`, `this.LOGIN_URL`), nothing appears to be wrong with your code, from what I can see. That all looks fine. What does your backend route look like? – justin.m.chase Jul 18 '16 at 21:03
  • @justin.m.chase I have a node server to get the requests. When i perform a Delete, for example, i receive normally: `::ffff:192.168.1.1 - - [18/Jul/2016:20:09:16 +0000] "DELETE /login HTTP/1.1" 200 12 "-" "okhttp/3.2.0"` For post/put requests nothing happen =/ – Lothav Jul 18 '16 at 21:08
  • Are you using express? You very well could have one of your routes misconfigured. – justin.m.chase Jul 18 '16 at 21:18
  • @justin.m.chase `var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); }); app.get('/login', function (req, res) { res.send('Hello World!'); }); app.delete('/login', function (req, res) { res.send('Hello World!'); }); app.post('/login', function (req, res) { res.send('Hello World!'); }); app.listen(3000, function () { console.log('Example app listening on port 3000!'); });` most simple as possible – Lothav Jul 18 '16 at 21:36
  • Did you get this resolved? I've been seeing a bug with ng2 and sending data in the body that's not a string. I realize you're stringifying the object but the header is making it break, I think. – Aarmora Nov 28 '16 at 22:33
  • All `http` calls return an observable. Are you sure you don't have to call `subscribe` on them before calling `map`? – Alex Florin Jul 19 '17 at 15:43
  • Any answer here? – CREM Sep 13 '17 at 13:03
  • Solve here : need to subscribe : https://stackoverflow.com/questions/41381200/angular-2-http-get-not-getting – Portekoi Apr 02 '18 at 11:32

0 Answers0