【发布时间】:2018-06-24 02:05:53
【问题描述】:
我正在尝试将从 php 返回的数据作为 JSON 数组显示到我的组件中。
在这个ngOnInit()我添加了以下脚本:
ngOnInit(){
this.http.get('http://aff.local/getPartners.php').subscribe(
(response: Response) =>{
const data = response.json() as Partners;
console.log(data);
//this.keys.push(this.data);
},
error =>{
console.log('error');
}
)
}
PHP 脚本如下:
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
require_once("./serverConnection.php");
class affiliatePartner
{
public function getPartners(){
global $conn;
$result = array();
$getPartners = "SELECT partner_name FROM partner";
$execGetPartners = $conn->prepare($getPartners);
$execGetPartners->execute();
$result = $execGetPartners->fetchAll();
// foreach($result as $res)
// {
// $res=array('partner_name'=>$result['partner_name']);
// }
return json_encode($result);
}
}
?>
我得到了以下结果:
然后,我想在一个 div 中显示 Udemy 和 Nike:
<ul *ngFor="let datas of data;">
{{datas}}
</ul>
但组件中没有出现任何内容。
我稍微搜索了一下,有人用了一个接口,所以我创建了一个接口:
import { Injectable } from '@angular/core'
export interface Partners{
name: String;
}
在主要组件中:
import { Partners } from './Interfaces';
我把脚本改成:
ngOnInit(){
this.http.get('http://aff.local/getPartners.php').subscribe(
(response: Response) =>{
const data = response.json() as Partners;
console.log(data.name);
//this.keys.push(this.data);
},
error =>{
console.log('error');
}
)
}
但我的 div 中仍然没有显示结果。
【问题讨论】:
标签: php json angular interface ngfor