0

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 ?

1 Answer 1

1

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';
Sign up to request clarification or add additional context in comments.

5 Comments

It's same, maybe I imported something wrong ? I use import { HttpModule } from '@angular/http';
is your API expecting content-type of application/json? Are you sure your data is coming through? Are you sure your API is expecting the same model as data?
The API it's in Laravel and expect Post data, whatever, I should see in Inspect Element -> Network the POST method
before the line return this.http.post(url, data) add a console.log(data) and make sure that it is correct
Object { name: "pula mea", email: "e mare", password: "foarte mare" }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.