如何使用多个参数在angular5中发送POST?

dEs*_*ZER 5 httpclient angular2-services angular angular5

我在 angular5 简单项目(前端)和后端(弹簧引导)中工作,我想向带有 2 个参数 idPerson 和 idProject 的 api rest 发送 post 请求,因此 api rest 可以影响项目到选定的人,我尝试在带有 POST 的服务中执行此操作,但这是不可能的。

这是 ProjectService.ts 的代码

    addProjToClient(idPerson:number,idProject:number){
    if(this.authService.getToken()==null) {
      this.authService.loadToken();
    }
    return this.http.post(this.host+"/saveProjectToClient",idPerson,idProject,{headers:new HttpHeaders({'Authorization':this.authService.getToken()})});

  }
Run Code Online (Sandbox Code Playgroud)

不可能在 http Post 中发送超过 2 个参数,我使用 httpClient 。

关于如何做到这一点的任何想法?

Ted*_*rne 5

的第二个参数http.post是发布请求的正文。只需将两个值放入正文中,然后将它们从服务器上的正文中取出即可。

return this.http.post(this.host+"/saveProjectToClient", {
  idPerson,
  idProject,
}, {
  headers:new HttpHeaders({
    'Authorization': this.authService.getToken()
  })
});
Run Code Online (Sandbox Code Playgroud)

在服务器上(Springboot)

public class Dto {
    private String idPerson;
    private String idProject;
}


@Controller
@RequestMapping("/")
public class ExampleController {

   @PostMapping("/saveProjectToClient")
   public ResponseEntity postController(@RequestBody Dto dto) {
      System.out.print("Person Id was: ");
      System.out.println(dto.idPerson);
      System.out.print("Project Id was: ");
      System.out.println(dto.idProject);

      return ResponseEntity.ok(HttpStatus.OK);
    }
}
Run Code Online (Sandbox Code Playgroud)