Request HTTP Ionic 2 GET

Asked

Viewed 527 times

0

Good afternoon guys, 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’s written on the page and display on an Alert. How can I do this ?

1 answer

0


Simple, insert the angular Http and then make the request. After that, just make an Alert using some Ionic Component.

Depending on the website you are requesting, you may happen to 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.

Browser other questions tagged

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