Angular 2 Error HTTPResponse not found

0

I'm trying to implement a login screen that accesses an api through a server, I need to pass the user's email and password to the api, and it returns me NULL on error or a number on success ( id of the user).

I'm getting the following message when I click the submit button:

HttpErrorResponse {headers: HttpHeaders, status: 404, statusText: "Not Found",url: "https://dev.endereco/login/undefined", ok: false, …}

I also get the following message:

POST https://dev.endereco/login/undefined 404 (Not Found)

Here are some important parts of my code:

login.component.html:

<input type="text" required name="email" [(ngModel)]="usuario.email" #nome="ngModel" id="usuario" placeholder="Email">

<input type="password" required name="password" [(ngModel)]="usuario.password" #senha="ngModel" id="senha" placeholder="Senha">

<button type="submit" name="entrar" (click)="fazerLogin(usuario.email, usuario.password)" class="login" id="login-button">Entrar</button>

login.component.ts:

private usuario: Usuario = new Usuario(); //Usuario é meu tipo de dado, foi declarado uma interface que possui o email e o password.

 public idUsuario: any = null;

 public alertaUsuarioIncorreto: boolean = false;

 constructor(private router: Router,
              private authService: AuthService) { }

  fazerLogin(usuario: Usuario): void{
    this.authService.fazerLogin(usuario.email, usuario.password)
      .subscribe(user => this.idUsuario = user);

      if(this.idUsuario != null){ //se for diferente de null faz login
        this.alertaUsuarioIncorreto = false;
        this.router.navigate(['/home'])
      }else{ //caso contrario mostra um toast com msg de erro
        this.alertaUsuarioIncorreto = true;
        var x = document.getElementById("toast")
        x.className = "show";
        setTimeout(function(){ x.className = x.className.replace("show", ""); }, 5000);
      }
    }
}

auth.service.ts:

fazerLogin(email: string, password: string): Observable<Usuario>  {
    return this._http.post<Usuario>(AppSettings.API_ENDPOINT+email,password)
  }

@Edit:

I tried to add a header provided by the Angular (?):

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json',
    'Authorization': 'my-auth-token'
  })
};

passing the header as a function parameter:

 fazerLogin(email: string, password: string): Observable<Usuario>  {
    return this._http.post<Usuario>(AppSettings.API_ENDPOINT+email,password, httpOptions)
      }

Now I get:

: Response for preflight has invalid HTTP status code 404.

HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …}
    
asked by anonymous 03.07.2018 / 02:53

1 answer

1

As @Eduardo Vargas mentioned in the comments, his login.component.html passes two parameters to the 'doLogin' function, since it only receives one. We can note that it is not interpreting past value if we analyze the url generated:

https://dev.endereco/login/UNDEFINED

To solve the problem, simply pass the whole object as a parameter:

(click)="fazerLogin(usuario)"

By doing so, your component gets the full user with all the properties.

    
03.07.2018 / 12:31