Bad Request 400: When accessing API with Ionic 3

Asked

Viewed 208 times

-1

I’m getting on my console the Bad Request 400 response to my API call:

inserir a descrição da imagem aqui

My app.api.ts in the app folder is:

export const MEAT_API = 'http://localhost:1337'

And my main code is:

submit(){
    var link = 'http://localhost:1337/user/login';
    var data = '?email='+JSON.stringify(this.usuario.email)+'&senha='+ JSON.stringify(this.usuario.senha);


    this.http.post(link, data)
      .subscribe(data => {
       this.data.response = data._body;

       if(this.data.response != "[]"){
        var resposta = this.data.response;
        console.log(resposta);

        // tinha que pegar o ID do usuário.... :/
         sessionStorage.setItem("usuarioEmail", this.usuario.email);
         //sessionStorage.setItem("idUsusario", resposta[0].idusuarios);
         sessionStorage.setItem("flagLogado", "sim");

         this.navCtrl.setRoot(WellFitPage, {}, {animate: true, direction: "forward"});
        }else{
          let alert = this.alertCtrl.create({
            title: 'Usuário Não encontrado!',
            subTitle: 'Verifique se digitou seu e-mail e senha corretamente.',
            buttons: ['OK']
          });
          alert.present();
       }
    })
  }

I am migrating from Ionic 1 to 3 a lot has changed. What can it be? My API was developed using Sailsjs.

In my Postman, it seems right, see:

inserir a descrição da imagem aqui

1 answer

0

Try it this way:

const link = 'http://localhost:1337/user/login';
const options =
   { 
       params: new HttpParams()
                  .set('email', this.usuario.email)
                  .set('senha', this.usuario.senha)
    };


    this.http.post(link, options )

A tip usually vc returns the observable of your service and subscribe to the component.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.