我是Angular2的新手。我有一个PHP文件,想从Angular组件中的PHP文件中获取内容。以下是我通过参考编写的代码。我无法弄清楚问题是什么,因为我没有在控制台或期望的输出中出现任何错误。
PHP代码 - ' getArea.php'
<?php
$link = mysql_connect('localhost', 'root', '');
mysql_select_db("mydb", $link);
$data=array();
$result = mysql_query("SELECT * FROM dam_color", $link);
$num_rows = mysql_num_rows($result);
for ($i=0; $i<mysql_num_rows($result); $i++)
{
$row = mysql_fetch_assoc($result);
$data['success'] = true;
$data['areaname'] = $row['area_name'];
}
echo json_encode($data);
?>
Angular Component - php-form.component.ts content
export class PhpFormComponent {
msg = "Welcome";
public data;
constructor(private http:Http){ }
ngOnInit(){
this.getData();
}
getData(){
this.http.get('http://localhost/myapi/getArea.php')
.map(res => res.json())
.map(res => {
if(res.success)
{
this.msg="Fetched data";
}
else
{
this.msg="Error in fetching data";
}
})
.subscribe(
data =>this.getdata = JSON.stringify(data),
err => console.error(err),
() => console.log('done')
);
}
}