首页 > 解决方案 > 在 localhost:4200 中找不到 404

问题描述

我正在尝试将数据从“ http://localhost:8082/api/kanchiwork/ ”传递到“ http://localhost:4200/ ”。我的 proxy.config.json 文件是:

    {
      "context": [
        "/api/**"
      ],
      "pathRewrite": {
        "^/api": ""
      },
      "ws": false,
      "target": "http://localhost:8082",
      "secure": false,
      "changeOrigin": true,
      "logLevel": "debug"
    }
  ]

我的服务文件如下。

import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class PeopleService {

  constructor(private http:HttpClient) { }

  fetchPeople(): Observable<Object>{
    return this.http.get('/api/kanchiwork/');
    //return this.http.get('assets/response.json');
  };

}

. 当我尝试传递值时,在控制台中收到消息为“GET localhost:4200/api/kanchiwork 404(未找到)”。

标签: angularapiangular-cli

解决方案


{
  "/api": {
    "target": "http://localhost:8082",
    "secure": false
  },
  "logLevel": "debug"    
}

您可以运行 ng serve --proxy-config proxy.conf.json

否则你可以更新你的 package.json 脚本属性:

"scripts": {
    "ng": "ng",
    "start": "ng serve --proxy-config proxy.conf.json",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  }

然后运行 ​​npm start

注意:如果您更改 proxy.conf 中的任何内容,您需要重新运行(ng serve/npm start)您的应用程序以查看实际更改。


推荐阅读