Estoy tratando de hacer una petición AJAX
desde Ionic
, tengo este html:
<ion-header>
<ion-navbar>
<ion-title>
Prueba AJAX
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<div center text-center>
<button ion-button (click)="traerInfo()">
Informacion ajax
</button>
</div>
</ion-content>
Y tengo el siguiente .ts
:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { AlertController } from 'ionic-angular';
@Component({
selector: 'page-prueba',
templateUrl: 'prueba.html'
})
export class PruebaPage {
constructor(public navCtrl: NavController, public alerCtrl: AlertController) {
}
traerInfo() {
$http.get("https://www.w3schools.com/xml/ajax_info.txt")
.then(function(response) {
let alert = this.alerCtrl.create({
title: 'Informacion obtenida!',
message: response,
buttons: ['Ok']
});
alert.present()
});
}
}
Me está tirando error de que la variable $http
no está definida. Pero ya probé incluyéndola en el constructor
:
constructor(public navCtrl: NavController, public alerCtrl: AlertController, public http: $http)
O incluso haciendo lo mismo importando todo como explican acá:
import { Http, Response } from '@angular/http';
Y haciendo en el constructor:
constructor(public navCtrl: NavController, public alerCtrl: AlertController, public http: Http)
Pero tampoco resultó...
Se me agotaron las opciones, desde ya muchas gracias!
import { Http, HttpModule } from '@angular/http';
y luegoconstructor(http: Http)
chequea que esta la Http esta en mayuscula la primera ademas que en el constructor cuando lo haces de esa forma esta haciendo estothis.http = http;
y cuando llames la funcion get debes hacerlo conthis.http.get
; dime si te funciono para agregarlo como respuesta @Genarito