【问题标题】:GET http://localhost:4200/profile/1 404 (Not Found)GET http://localhost:4200/profile/1 404(未找到)
【发布时间】:2021-06-28 12:08:31
【问题描述】:

profile.service.ts

  getProfile(userId:number): Observable<UserModel> {
    return this.httpClient.get<UserModel>('/profile/inquireUserInfo/' + userId);
  }

profile.component.ts

  private getProfile() {
    this.profileService.getProfile(this.userId).subscribe(data => {
      this.userModel = data;
    }, error => {
      console.log(error);
    });
  }

proxy.config.json

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

package.json

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

但是,当我运行此应用程序时,我收到如下图所示的 404 错误:

【问题讨论】:

  • 您是否检查了传递给服务类的 this.userId 的值。 URL 末尾显示未定义。
  • getProfile(userId:number): Observable&lt;UserModel&gt; { return this.httpClient.get&lt;UserModel&gt;('/profile/inquireUserInfo/1'); } 所以它给出了同样的错误。

标签: angular api get


【解决方案1】:

您的服务似乎没有发送api/profile/...,因为您的代理将api 路由到后端服务器。

【讨论】:

  • 是的,我认为这是问题所在。试试这个网址:'/api/profile/inquireUserInfo/' + userId
  • 感谢您的帮助。工作。 @GetMapping(path = "/api/profile/{userId}"
  • 简单但常见的问题。 :) 放轻松。
猜你喜欢
  • 2018-08-01
  • 2018-11-10
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
  • 2020-12-01
  • 2019-02-04
  • 1970-01-01
相关资源
最近更新 更多