在 localhost:4200 中未找到获取 404

Getting 404 not found in localhost:4200

我正在尝试传递来自“http://localhost:8082/api/kanchiwork/" to "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 (Not Found)"。

proxy.conf.json 试试这个:

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

}

现在 运行 使用此命令的应用程序:

ng serve --proxy-config proxy.conf.json
{
  "/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) 您的应用程序才能看到更改的效果。