0

I am just trying out this example to learn more about ionic 2. I just want to fetch and display the data in my ionic app.I am using mySQL database and PHP.When i run ionic serve, the data is fetched without any errors but i am just getting empty homepage. I have added my home.ts..,home.html and test.php file. Can anyone suggest me what am i doing wrong ? Thank you.

HOME.ts

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

 @Component({
 selector: 'page-home',
 templateUrl: 'home.html'
 })
 export class HomePage {
 posts: any;
 constructor(public http: Http ) {
 this.http.get('http://192.000.00.000/test.php').map(res =>     res.json()).subscribe(data => {
    this.posts = data.data.children;
},

 err => {
    console.log("Oops!");
});
}
}

 home.html

 <ion-header>
 <ion-navbar>
 <ion-title>Home Page</ion-title>
 </ion-navbar>
 </ion-header>

 <ion-content class="home page" >
 <ion-list>
 <ion-item *ngFor="let post of posts">
 <img [src]="post.data.url" />
 </ion-item>
 </ion-list>
 </ion-content>

 test.php

  <?php
  $output = array(
    'success'=>'yes',
    'data'=>555
    );

  echo json_encode($output);
  ?>

enter image description here

3
  • 1
    this.posts = data.data.children;.. your data does not have children property.. so posts is undefined. Commented Apr 4, 2017 at 11:52
  • are you sure you have shown the right php code? Commented Apr 4, 2017 at 11:52
  • @suraj yes..,that is the right php code Commented Apr 4, 2017 at 11:56

1 Answer 1

1

Your output from your php script would be

{
  "success": "yes",
  "data": 555
}

In your subscribe method you try to access data.data.children. Since 555 is not an object with a property named children your posts variable will be undefined.

Therefore your page is home.html page is displaying the "correct" data.

I'm not that familiar with PHP since I don't have a parser handy, but try to output a value that has a children property instead

$output = array(
'success'=>'yes',
'data'=>array(
  'children'=>array(
    array('data'=>array('url'=>'http://www.example.com'))
  ))
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.