Angular - how to get variable from local PHP file -
i want variable getdata.php file in src/assets folder of angular project.
<?php ... echo json_encode('test'); ?>
get-data.service :
import { injectable } '@angular/core'; import { http, response } '@angular/http'; import { observable } 'rxjs/rx'; import 'rxjs/add/operator/map'; @injectable() export class getdataservice { constructor(private http: http) {} gettest(): observable<any> { return this.http.get('assets/getdata.php') .map(response => response.json()); } }
app.component :
import { component } '@angular/core'; import { getdataservice } './services/get-data.service'; @component({ selector: 'app-root', templateurl: './app.component.html', styleurls: ['./app.component.css'] }) export class appcomponent { constructor(private getdataservice: getdataservice) { } title = 'run chart generator'; data; gettesttwo() { this.getdataservice.gettest() .subscribe(data => { this.data = data; console.log(this.data) }); } }
when call function gettesttwo i've got:
syntaxerror: json.parse: unexpected character @ line 1 column 1 of json data
when change php echo json_encode('test')
echo 'test'
, service .map(response => response.json())
.map(response => response)
i've got in console:
object { _body: "<?php include('simple_html_dom.ph…", status: 200, ok: true, statustext: "ok", headers: object, type: 2, url: "http://localhost:4200/assets/getdat…" }
how can retrieve variable php file?
@angular/cli: 1.4.1
@angular/core: 4.3.6
your php file won't work under nodejs project. need separate angular app server logic. use nginx or apache serve php file, able call in gettest() function.
Comments
Post a Comment