George Hulpoi
George Hulpoi

Reputation: 667

Angular 4: HTTP doesn't sent POST data

I trying to make a Web Application and I want to test the HTTP functions, but I have a problem.

For example: I have this:

import { Injectable } from '@angular/core';

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

import {Observable} from 'rxjs/Rx';

// Import RxJs required methods
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

@Injectable()
export class UserService {

    constructor(private http: Http) { }

    create(data: Object): Observable<any> {

        let headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded'}),
              options = new RequestOptions({ headers: headers});

        return this.http.post('http://localhost:8000/api/signin', {headers:headers}).map((res) => { res.json(); alert('ma-ta'); }).catch((error:any) => Observable.throw(error.message));
    }    
}

The function is called, I verified, but the http.post doesn't work, I mean, it not sent the data to that link, in Console I have nothing at Network, and I receive this:

12:01:18.779 Object { _isScalar: false, source: Object, operator: Object } 1 main.bundle.js:444:9

It's a kinda empty, only things I don't need, like an error information or something. Can somebody help me ?

Upvotes: 0

Views: 1050

Answers (1)

Wesley Coetzee
Wesley Coetzee

Reputation: 4848

You are sending an empty post. You were setting the headers (which isn't needed) and not sending the data through with the post request. Please see below:

create(data: Object): Observable<any> {
  let url: string = 'http://localhost:8000/api/signin';    
  return this.http.post(url, data)
    .map((res: Response) => res.json())
    .catch((error:any) => Observable.throw(error.message));
}

also remember to import the following:

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';

Upvotes: 1

Related Questions