0

I'm new to angular 2.I'm using http.get() for fetching a data from php file.this is my php file

<?php
header("Access-Control-Allow-Origin:*");
header('Access-Control-Allow-Headers: X-Requested-With');

include('connection.php');
$fd=new Connect();
$fd->__construct();

$data=array();

    $select=mysql_query("SELECT * FROM userData'")or mysql_error();
    $sql=mysql_num_rows($select);
    if($sql>0)
    {
        while($row=mysql_fetch_array($select))
        {

        }
    }

header('Content-Type: application/json');

    echo json_encode($data);
?>

Here I used

header("Access-Control-Allow-Origin:*");
header('Access-Control-Allow-Headers: X-Requested-With');
header('Content-Type: application/json');

to get this php file in json file still it's not working.I got whole php file in response instead of json file. this is my angular 2 component part

export class LoginComponent {
    private data;

    getData:string;

   constructor (private http:Http){}

   Getlogin(){
    //this.httpService.getUserData()

    this.http.get('dev/AngularWithPhp/login.php')
        .map(res => res.json())
        .subscribe(
                data=>this.getData = JSON.stringify(data),
                error =>alert(error),
                ()=>console.log("done"));
    }    

So what's the problem?give me suggestions...

Krishna Patel
  • 275
  • 5
  • 19
  • Are you sure that your php server is runing? Your request from Angular component looks good. – Sefa Oct 12 '16 at 06:05
  • yes its running.php and angular files are on same sever. – Krishna Patel Oct 12 '16 at 06:23
  • What do you get when you try to access it not from angular? 100% sure that php server is running? – jmachnik Oct 12 '16 at 06:36
  • I get my whole php file as in text form.And I'm sure that php server is running.@jmachnik – Krishna Patel Oct 12 '16 at 06:41
  • I am not a php developer, but isn't that indicates that the server IS NOT running? – jmachnik Oct 12 '16 at 06:45
  • 1
    @KrishPatel the server is running but not setup to properly handle PHP files (server doesn't know that it is a script that should be run; thinks it's just a resource to send to the browser). Tell us a bit about your server setup as it relates to PHP. This is not an Angular problem. – BeetleJuice Oct 12 '16 at 06:46
  • @BeetleJuice my php file is in localhost:3000 the default server of angular 2.GET http://localhost:3000/widgets/AngularWithPhp/login.php this is where my php file is.so what's now did I need to change something? – Krishna Patel Oct 12 '16 at 06:53
  • [This is the problem you face](http://stackoverflow.com/questions/18422140/apache-is-downloading-php-files-instead-of-displaying-them). Look on that page for solutions – BeetleJuice Oct 12 '16 at 07:53

0 Answers0