Ionic 2, Making a HTTP request to get JSON Data From Backend Server

In this tutorial we are going to load some JSON (javascript object notation) data in our Ionic 2 application.

Objectives of this tutorial

  1. Generate Ionic 2 app using Ionic CLI
  2. Serve the project and view in our browser
  3. Create a provider using Ionic CLI
  4. Use Http service to perform Get request and subscribe to return data
  5. Use ngFor and ngIf angular directive in our view to display our data

Let’s begin by generating an ionic 2 project using Ionic CLI. If you are unfamiliar with creating Ionic 2 apps using Ionic CLI read our getting started tutorial.

$ ionic start HttpProject --v2
$ cd HttpProject

This is will create a new Ionic 2 project in the folder HttpProject with Ionic tabs starter. To check that your project is properly created serve the project in the browser.

$ ionic serve


You should be given a server url in your command window generally its http://localhost:8100, opening the link in the browser should give you following view



Final result of this tutorial can be found at the following repo


All the work related to talking to backend server is done in the provider. It’s a angular 2 concept. In this tutorial we are going to load JSON data from reddit server into our application. Let’s generate a provider to handle talking to reddit server using Ionic CLI.

$ ionic g provider RedditService

Open up your project in your favourite text editor. We are going to use atom. In your projects ‘/src/provider/’ open reddit-service.ts in the editor and paste the following code.

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

export class RedditService {

 constructor(public http: Http) {
 console.log('Hello RedditService Provider');

Here, we are importing Injectable from angular core to make this an injectable service. Then we are importing Http from angular http that allows us to make http requests. We are also importing rxjs map operator that will allow us to map our http response string to JSON object.

We have added a new function getRemoteDate(), that performs http get request and print’s it in the console. In order for us to be able to use this provider, we need to add this to the app module, that is our default module for ionic project. Open ‘src/app/app.module.ts’ and paste the following line

import {RedditService} from "../providers/reddit-service";

below other import statements, and add RedditService to the providers array as

providers: [
 {provide: ErrorHandler, useClass: IonicErrorHandler},

Now, to call the function we just created in the RedditService, we need to call it from our page. We are going to do that from our home page. Open ‘src/pages/home/home.ts’ and paste the following code

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {RedditService} from "../../providers/reddit-service";

 selector: 'page-home',
 templateUrl: 'home.html'
export class HomePage {

 constructor(public navCtrl: NavController, public redditService:RedditService) {

All we did here is, added an import statement to import our provider, and instantiated our provider in the constructor, and in the ionViewDidLoad() function called our getRemoteData() function. If you have been following the tutorial, your app should already be served in the browser and it should automatically reload to reflect the change. You will not see any change in the view, but you should see the log we printed in the console. Open up your browser console and you should see something like below


Our function is making an Http request and returning an observable. But this is not what we want, we want our JSON data.

An observable is a thing that we can subscribe to, that will emit data over time. So an observable can say something at any time and we must be listening to catch what that observable said. To learn more about observable follow the link.

Change the code of our getRemoteData() function as follows and view console to see what happens


You should see the following output in the console.


Still not exactly what we want. This is an Http request, if we look in, we can find the actually data in this response. This is where the map comes in. We can use map operator to change the response object to get only the data. Change the code of the getRemoteData() function again and view the result.


This should print the actual JSON object in the console as follows.


Finally, this looks like the data we want.

For the last part, we will be displaying the gif in our home page. To do that, change the code of the getRemoteData() function as follows

 return this.http.get('https://www.reddit.com/r/gifs/top/.json?limit=10&sort=hot')

Here, we are returning the observable. Now change the HomePage class in home.ts as follows:

export class HomePage {
 private data:any;
 constructor(public navCtrl: NavController, public redditService:RedditService) {

Here, we are making a private property named data, and ionViewDidLoad() is subscribing to our Http request and returning data to our data property.

Finally change the code of ‘src/pages/home/home.html’ as follows to show our gifs


<ion-content padding>
 <h2>Welcome to Ionic!</h2>
 <div *ngIf="data != undefined">
 <div *ngFor="let gif of data.children, let i = index">
 <ion-item *ngIf="gif.data.url.substring(gif.data.url.lastIndexOf('.'),gif.data.url.length)==='.gif'">
 <img [src]="gif.data.url" />

Here, first we are only showing the items if data != undefined. Then using ngFor directive, we are looping through the children object our data and we are only showing the item if the extension is “.gif”. Finally you should see the following output, the gif images may be different than mine.


If your not seeing the images, log your data in console and check to see if there are any .gif urls in the data returned by reddit service.

That’s it, if you have any queries or confusion, feel free to comment. Thank you.

Related Posts

%d bloggers like this: