向Angular4中的PHP端点请求



所以我是Angular4的新手,我有一项应该向PHP API提出请求的服务。我有以下代码 -

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

我将获得404 Localhost:4200/weather.php找不到。不确定要放置PHP端点文件。

假设Localhost:4200是您的前端服务器,则必须在呼叫中设置PHP服务器API路径。如果您的php服务器在8080端口的本地中,则键入this.http.get('http://localhost:8080/weather.php',...

您可以将其放在src/assets文件夹中,然后使用this.http.get("assets/weather.php", {...调用。

这是因为Angular应用程序托管在4200端口上,您的PHP文件将使用端口80。

尝试使用

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

最新更新