Call PHP file in Angular2

ぐ巨炮叔叔 提交于 2019-12-12 12:25:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!