0
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';

// import 'rxjs/add/operator/toPromise';


@Injectable()
export class AccessService {
private baseUrl = '';

constructor(private http: Http) {

}

login(email: string, password: string) {
    //      var loginUrl = 'http://localhost:8000/app.php/api/login_check';  // url to web API
    var loginUrl = this.baseUrl + 'api/login_check';  // url to web API
    let user = JSON.stringify({ _username: email, _password: password });
    console.log(user);
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });


    return this.http
        .post(loginUrl, user, options)
        .map(this.extractData)
        .catch(this.handleError);
}
register(email: string, password: string, password2: string) {
    var loginUrl = this.baseUrl + 'register/';  // url to web API
    var fos_user_registration_form = {
        username: email,
        email: email,
        plainPassword: { first: password, second: password2 },
        roles: ['ROLE_CLIENT']
    };
    let user = JSON.stringify({
        fos_user_registration_form

    });
    console.log(user);
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    if (password === password2) {
        return this.http
            .post(loginUrl, user, options)
            .map(this.extractData)
            .catch(this.handleError);
    } else {
        alert('Las Contraseñas No Coinciden');
    }
}

private extractData(res: Response) {
    let body = res.json();
    console.log(body);
    return body.data || {};
}
private handleError(error: Response | any) {
    // in a real world app, we might use a remote logging infrastructure
    let errMsg: string;
    if (error instanceof Response) {
        const body = error.json() || '';
        const err = body.error || JSON.stringify(body);
        errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
        errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg.toString());
    return Observable.throw(errMsg);
}

Im trying to make a simple post with angular 2 but return this exception EXCEPTION: this.http.post is not a function, im not sure that is wrongly, help me please. Before it worked but I clone the project in another folder and this does not work for me.

1 Answer 1

2

Most likely you're not providing the HttpModule in your app.module.ts (or whichever module this is a part of). Make sure you have the following:

import { HttpModule } from '@angular/http';

@NgModule({
  declarations: [
    ....
  ],
  imports: [
    HttpModule,
    ...
  ],
  ...
});
Sign up to request clarification or add additional context in comments.

Comments

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.