Request HTTP ionic 2 GET

0

Good afternoon guys, I use ionic 2 and how can I make a request get (without provider nd only in class ts)? I just need you to take what is written on the page and display it on an alert. How can I do this?

    
asked by anonymous 14.11.2016 / 16:19

1 answer

0

Simple, inject the Http of angular and then make the requisition. After that, just do an alert using some ionic component.

Depending on the site you are requesting, you may have a problem with CORS ( related topic ).

Example:

import { Http, Response, URLSearchParams } from '@angular/http';
import { AlertController } from 'ionic-angular';

constructor(
        private http: Http, 
        private alertCtrl: AlertController) { }

requisicao() {
    let params: URLSearchParams = new URLSearchParams;
        params.set('parametro1', 'bla');
        params.set('parametro2', 'bla2');

    this.http.get('http://..., {
        search: params
    }).toPromise().then(response => {
        let alert = this.alertCtrl.create({
            title: 'Good Lock!',
            subTitle: response,
            buttons: ['OK']
        });

        alert.present();
    });
}

Note: Remembering that doing everything together (in the same class) can make your code more coupled and less cohesive. Try to properly separate class responsibilities.

    
29.01.2017 / 17:45