How to use / make Http GET request Ionic2


#1

Hi,

I’m implementing an app that will need to make Http requests (GET and POST) and I’m trying to implement a simple Http GET Request, but can’t get it to work.

I have a provider that will handle the http request, that should be imported in a page.

myHttpProvider.js

import {Injectable, Inject} from 'angular2/core';
import {Http} from 'angular2/http';
import 'rxjs/add/operator/map';

@Injectable()
export class MyHttpProvider {
  static get parameters(){
    return [[Http]];
  }

  constructor(http) {
    this.http = http;
    this.data = null;
  }

  load() {
    if (this.data) {
      // already loaded data
      return Promise.resolve(this.data);
    }

    // don't have the data yet
    console.log('making request');
    return new Promise(resolve => {
      this.http.get('BitTorrent.json').subscribe(res => {
        this.data = this.processData(res.json());
        console.log(this.data);
        resolve(this.data);
      },
      err => {
        console.log('ERRRRR');
      });
    });
  }
}

And my view class:

import {Page, Alert, NavController} from 'ionic-angular';
import {MyHttpProvider} from '../../providers/google-provider/google-provider';

@Page({
  templateUrl: 'build/pages/getting-started/getting-started.html',
  providers: [MyHttpProvider]
})

export class GettingStartedPage {
	static get parameters(){
		return [[NavController],[MyHttpProvider]];
	}
	constructor(nav, myHttpProvider) {
		this.nav = nav;
		this.load = myHttpProvider.load;
	}
    }

As soon as I click on a button that will call the load function:

<button block wout (click)="load()">make get reqt</button>

I only get “making request” as output to console.
On Chrome inspect console, no request is made, and I don’t get any error at all.

What am I missing?
I’me new to both Ionic and AngularJs, I’ve read documentation and examples but no success yet.

Thanks in advance