adrian
adrian

Reputation: 21

Ionic app getting data from url json

I don't know how to do this. I tried few examples from internet. What is the best solution?

import { Component } from '@angular/core';

@Component({
  selector: 'page-hello-ionic',
  templateUrl: 'hello-ionic.html'
})
export class HelloIonicPage {
  constructor() {

  }
}

Upvotes: 2

Views: 11230

Answers (2)

Sanjiban Roy
Sanjiban Roy

Reputation: 1

To do this just use the below code, no need to import any module

fetch('https://url.com').then(res => res.json())
    .then(json => {
      
       console.log(json)  
 
 });

Upvotes: 0

Diluk Angelo
Diluk Angelo

Reputation: 1503

to do this

Modify src/app/app.module.ts and import

import { HttpModule } from '@angular/http';

And Add to the Imports

@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    BrowserModule,
    HttpModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

After that you can use the HTTP Module from angular.

In your Page.ts

import the module and use it like this

 import { Http } from '@angular/http';

 constructor(public navCtrl: NavController, public http: Http) {

   let url = "https://www.reddit.com/r/gifs/new/.json?limit=10";

    this.http.get(url).map(res => res.json()).subscribe(data => {

        console.log(data);

    });

  }

Additionally you can convert to returned JSON string to JSON Object

var jsonobject = JSON.parse(data)

And Alternatively you can use the IONIC NATIVE HTTP PLUGIN

Cheers :D

Upvotes: 1

Related Questions