-1

I have rest call like below. Its like cross domain. I want to call the php file which is in the same project folder. But I am getting 404 error. I can able to download the file using error link. That means link what I am giving is correct.

return this._http
    .post('/app/php/reports.php', data.toString(), {headers: this.headers})
    .toPromise()
    .then(res => res.json().data)
    .catch(this.handleError);

Header is defined below. private headers = new Headers({'Content-Type': 'application/json'});

Imported files are listed below

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

 import 'rxjs/add/operator/map';
 import 'rxjs/add/operator/toPromise';

Is there any dependencies for npm server to call the php file. I have updated the node sever with "sudo npm install php-embed" Still I am facing this issue. I hope its a problem with node server to call the .php file. I can able to call the xml file using get request. Here I want to post the data to php file.

raj m
  • 1,863
  • 6
  • 21
  • 37

1 Answers1

1

Angular2 uses lite-server its default port is 3000. Enter the host as wamp/lamp server path (where the php files you placed). Example: localhost,localhost:80 or localhost:8080 Check the below code.

return this._http
    .post('http://localhost/app/php/reports.php', data.toString(), {headers: this.headers})
    .toPromise()
    .then(res => res.json().data)
    .catch(this.handleError);
Libu Mathew
  • 2,976
  • 23
  • 30
  • Here php file in current folder structure only.. I didnt host any server. – raj m Oct 04 '16 at 08:17
  • Cant I call the same app folder php file using npm server ? – raj m Oct 04 '16 at 08:25
  • 2
    No . You need to install php compiler for execution. The simplest way is install WAMP/XAMPP/LAMP in your compute and place the php file in the www root directory and set the requesting url as the server path. eg. 'localhost/app/php/reports.php' – Libu Mathew Oct 04 '16 at 09:06
  • I have already same project running on MAMP and I have called the report.php file. But I am getting error like "XMLHttpRequest cannot load http://localhost/Projects/cwf-06072016/services/reports.php. Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response." – raj m Oct 04 '16 at 09:20
  • place the following code in your reports.php page. header('Access-Control-Allow-Origin: *'); http://stackoverflow.com/questions/7564832/how-to-bypass-access-control-allow-origin – Libu Mathew Oct 04 '16 at 09:23
  • Included.. Still I am facing this issue – raj m Oct 04 '16 at 10:43
  • So the client side working perfectly. Just provide your php code – Libu Mathew Oct 04 '16 at 10:48
  • only this much code also doesnt work – raj m Oct 04 '16 at 10:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124883/discussion-between-ti2005-and-raj-m). – Libu Mathew Oct 04 '16 at 10:58