1

Me gustaría llamar a un archivo php desde Angular pasando el parametro usuario y clave .

import { Component, OnInit } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
@Component({
  selector: 'app-api-login',
  templateUrl: './api-login.component.html',
  styleUrls: ['../app.component.css']
})
export class ApiLoginComponent implements OnInit {
  usuario:String;
  clave:String;
  register:Boolean;
  constructor() { 
    this.usuario="PrecaNuevo"
    this.clave="PrecaNuevo";
    this.register=false;
  }

  ngOnInit() {
    var conAjax = $http.post("pgular/php/login.php", {usuario: this.usuario, clave: this.clave});
    conAjax.success(function(respuesta){
        console.log(respuesta);
    });
  }

}

Recibo el error: ERROR ReferenceError: $http is not defined

He empezado hoy con Angular, seguro que faltan algunas cosas.

1 respuesta 1

0

Te recomiendo que eches un vistazo al tutorial de angular.

Así, de forma rápida, $http no existe en tu componente. Angular funciona sobre la inyección de dependencias, es decir, para utilizar el cliente http de angular lo debes inyectar en el constructor:

constructor(httpClient: HttpClient) {
    ...
}

Para hacer la llamada seria:

this.httpClient.post(URL, params...)...

Y para que esto funcione debes importar el módulo HttpClientModule en tu AppModule.

Pero lo más importante en mi opinión es que realices el tutorial que te he enlazado arriba.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.