Can't Login through API IONIC 3

Hi there,

I am new to IONIC and I am trying to build a simple application with a login through my API (JWAUTH Laravel).

I was following some tutorials and I can’t understand where my mistake is, when I try to login the code jump right to the error message.

Any ideas why ? Thanks.

AuthServiceProvider

import { HttpClient, Headers } from '@angular/common/http';

let apiUrl = "http://127.0.0.1:8000/api/";

...

export class AuthServiceProvider {

postData(credentials, type) {
      return new Promise((resolve, reject) => {
        let headers = new Headers();

        this.http.post(apiUrl + type, JSON.stringify(credentials), {headers: headers})
          .subscribe(res => {
            resolve(res.json());
          }, (err) => {
            reject(err);
          });
      });

    }

}


Login.ts

import { AuthServiceProvider } from '../../providers/auth-service/auth-service';

...

export class LoginPage {

responseData : any;
  userData = {"email":"", "password":""};

   constructor(public navCtrl: NavController, public menuCtrl: MenuController, public authServiceProvider : AuthServiceProvider) {
    this.menuCtrl.enable(false, 'myMenu');
  }
  goToHome(params){

    this.authServiceProvider.postData(this.userData, "auth/login").then((result) =>{
      console.log('YES!!!');
      this.responseData = result;
      console.log(this.responseData);
      localStorage.setItem('userData', JSON.stringify(this.responseData));
      this.navCtrl.push(HomePage);
    }, (err) => {
      console.log('erro');
      //Connection failed message
    });
  }

}

Any tutorial that recommends “new Promise()” when dealing with Angular HttpClient is braindead and should not be followed. In addition, you don’t need to be making headers, stringifying anything, or calling json() on responses. I would suggest starting over following the idioms documented in here.

If the problem still persists, with issues like this, it’s important to know where you are running the app and where you are running the server. 127.0.0.1 is where the app is running, so if that’s a device things aren’t going to work unless there’s a proxy running on there.

Thanks I will take a look.

The app and the server ar both local.

Hi, on postData() try use “resolve(res)” instead of “resolve(res.json())”