Angular 在Ionic 2中使用Http从服务器获取远程数据
我只是尝试这个例子来了解更多关于离子2。我只想在我的ionic应用程序中获取和显示数据。我使用的是mySQL数据库和PHP。当我运行ionic Service时,获取的数据没有任何错误,但我只是得到一个空主页。我已经添加了my home.ts..、home.html和test.php文件。谁能告诉我我做错了什么?多谢各位Angular 在Ionic 2中使用Http从服务器获取远程数据,angular,typescript,ionic2,Angular,Typescript,Ionic2,我只是尝试这个例子来了解更多关于离子2。我只想在我的ionic应用程序中获取和显示数据。我使用的是mySQL数据库和PHP。当我运行ionic Service时,获取的数据没有任何错误,但我只是得到一个空主页。我已经添加了my home.ts..、home.html和test.php文件。谁能告诉我我做错了什么?多谢各位 HOME.ts import { Component } from '@angular/core'; import { Http } from '@angular/htt
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);
?>
php脚本的输出将是
{
"success": "yes",
"data": 555
}
在subscribe方法中,您尝试访问data.data.children。由于555不是具有名为children的属性的对象,因此posts变量将是未定义的
因此,您的页面是home.html页面正在显示正确的数据
我对PHP不太熟悉,因为我手头没有解析器,但尝试输出一个具有children属性的值
$output = array(
'success'=>'yes',
'data'=>array(
'children'=>array(
array('data'=>array('url'=>'http://www.example.com'))
))
);
this.posts=data.data.children;。。您的数据没有子属性。。所以posts是未定义的。您确定显示了正确的php代码吗?@suraj yes..,这是正确的php代码