0

So i am new to Angular4, I have a service that is supposed to make a request to a php api. I have the following code -

getWeather(keyword: string){
  return this.http.get("weather.php",  {
    params: new HttpParams().set('keyword', keyword),
    headers: new HttpHeaders().set('Method', 'search'),
  });

I am getting a 404 localhost:4200/weather.php cannot be found. not sure wher to place the php endpoint file.

2
  • Where does the weather.php file sit on your application? Commented Nov 24, 2017 at 11:29
  • i put it in the /app folder Commented Nov 24, 2017 at 12:52

3 Answers 3

1

Assuming that localhost:4200 is your front-end server, you have to set your php server api path in the call. If your php server in in local on the 8080 port, you type this.http.get('http://localhost:8080/weather.php',...

Sign up to request clarification or add additional context in comments.

5 Comments

why use absolute urls?
@madalinivascu this is in case your api backend is different of your front end entry point. If you wand to use 4200 port, create a proxy middleware that redirects to your php server. With angular-cli: github.com/angular/angular-cli/blob/master/docs/documentation/…
I dont have a php server...its just a script file....how do i go about talking to the script from my angular 4 app
To execute you php script ou need a php server like easyphp. Where is located your file?
Ok i understand now...This is the correct answer because there is no way to run the php script without a server to seat infront of the script and accept requests.
0

You can put it in your src/assets folder and then call it using this.http.get("assets/weather.php", {....

Comments

0

This is because for Angular application is hosted on 4200 port and your PHP file will be using port 80.

Try using

http://localhot/path/to/weather.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.