问题
I am new to Angular2. I have one PHP file and would like to fetch content from PHP file in Angular component. Following is my code I have written by taking references. I am not able to figure out what is the issue as I am not getting any error in console nor desired output.
PHP code - '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')
);
}
}
来源:https://stackoverflow.com/questions/43319715/call-php-file-in-angular2