Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Angular2中调用PHP文件_Php_Angular_Angular2 Http - Fatal编程技术网

在Angular2中调用PHP文件

在Angular2中调用PHP文件,php,angular,angular2-http,Php,Angular,Angular2 Http,我是安格拉尔的新手。我有一个PHP文件,希望从PHP文件中获取Angular组件中的内容。以下是我通过引用编写的代码。我无法找出问题所在,因为我在控制台中没有得到任何错误,也没有得到所需的输出 PHP代码-“getArea.PHP” <?php $link = mysql_connect('localhost', 'root', ''); mysql_select_db("mydb", $link); $data=array(); $result =

我是安格拉尔的新手。我有一个PHP文件,希望从PHP文件中获取Angular组件中的内容。以下是我通过引用编写的代码。我无法找出问题所在,因为我在控制台中没有得到任何错误,也没有得到所需的输出

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);
?>

预期的结果是什么?实际发生了什么?取决于成功/失败,我只想显示消息。目前,它也没有抛出任何问题或正确的输出。@sabin尝试了您的代码,但无法复制该问题,请尝试在plunker中复制它好吗?解决了,这是由于InMemoryWebapModule和InMemoryDataService造成的。我对这段代码进行了注释,并获得了所需的输出。参考文献
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')
        );
    }
}