尝试连接到角度2应用程序中的PHP后端时未定义的值

时间:2017-06-24 21:07:02

标签: php angular observable

我正在尝试将我的角度应用程序与PHP连接。我收到了错误

echo $data

这是我的代码:

PHP:

Unexpected token < in JSON at position 0

服务:

<?php 
$successreturn[]=array(
        "id"=>"any",
       "firstname"=>"any",
      "lastname"=>"any",
    "dateofbirth"=>"any",
    "city"=>"any",
    "gender"=>"any");
header("Access-Control-Allow-Origin: *");
$servername="localhost";
$username="root";
$password="sandeepchetikam";
$dbase="mydb";
$conn=mysqli_connect($servername,$username,$password,$dbase);
if (!$conn) {
    echo "Connection Problem".mysqli_connect_error($conn);
}

$sql= "SELECT * FROM Employees";
$result = mysqli_query($conn,$sql);
$count = mysqli_num_rows($result);
    $value=0;
if(!$result){
    echo "Connection Failed " .mysqli_connect_error($result);
 }
 while($row = mysqli_fetch_assoc($result)){

    $successreturn[$value]['id']=$row['id'];
         $successreturn[$value]['firstname']=$row['firstname'];
         $successreturn[$value]['lastname']=$row['lastname'];
         $successreturn[$value]['dateofbirth']=$row['dateofbirth'];
         $successreturn[$value]['city']=$row['city'];
         $successreturn[$value]['gender']=$row['gender'];
         $value++;

        };
echo json_encode($successreturn);


?>

组件:

import {Injectable} from '@angular/core';
import {Http,Response,Headers,ResponseOptions} from '@angular/http';
import {Observable} from 'rxjs';
import {Data} from './data';

@Injectable()
export class DataService{
    empurl="http://localhost/php/connect-employee.php";
    constructor(private http:Http){}

    getDetailsFromDataBase():Observable<Data[]>{
        return this.http.get(this.empurl)
        .map(this.extractData)
        .catch(this.HandleError)
    }

    private extractData(res: Response){
            let body= res.json();
            return body.data() || []
    }
    private HandleError(error: Response| any){
        console.log(error.message || error);
                return Observable.throw( error.message || error);
        }
}

这是我的代码。我不知道我的PHP代码或服务中是否出错了。我想我没有从我的PHP文件中获取JSON数据。我怎么解决这个问题?任何帮助,将不胜感激。

0 个答案:

没有答案