如何在带有多个参数的angular5中发送POST?
How to send POST in angular5 with multiple params?
我在 angular5 简单项目(前端)和后端(spring 启动)中工作,我想发送一个 post 请求到 api rest with 2参数 idPerson 和 idProject,因此 api 其余部分会影响所选人员的项目,我尝试在 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()})});
}
在 http Post 中不能发送超过 2 个参数,我使用 httpClient 。
知道怎么做吗?
将所有参数添加到 json 对象中,例如:
{ idPerson: 'personId', idProject: 'projectId'}
作为第二个参数
尝试这样的事情:
generisiIOS(idtipkomitenta: number, idtipios: number, datumpreseka: Date, idkorisnika: number) {
let bodyString = JSON.stringify({ idtipkomitenta, idtipios, datumpreseka, idkorisnika });
let headers = new HttpHeaders({ 'Content-Type': 'application/JSON' });
return this._http.post<number>('faktura/generisiios', bodyString, { headers });
}
http.post
的第二个参数是 post 请求的主体。只需将两个值都放入正文中,然后在服务器上将它们从正文中取出。
return this.http.post(this.host+"/saveProjectToClient", {
idPerson,
idProject,
}, {
headers:new HttpHeaders({
'Authorization': this.authService.getToken()
})
});
在服务器上(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);
}
}
createRange(range: any, details: any): Observable {
let headers = new HttpHeaders().set('Content-Type', 'application/json');
let body = JSON.stringify({ 'range': range, 'details': details });
return this.httpClient.post(this.serviceURL + '/save', body, { headers });
}
我在 angular5 简单项目(前端)和后端(spring 启动)中工作,我想发送一个 post 请求到 api rest with 2参数 idPerson 和 idProject,因此 api 其余部分会影响所选人员的项目,我尝试在 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()})});
}
在 http Post 中不能发送超过 2 个参数,我使用 httpClient 。
知道怎么做吗?
将所有参数添加到 json 对象中,例如:
{ idPerson: 'personId', idProject: 'projectId'}
作为第二个参数
尝试这样的事情:
generisiIOS(idtipkomitenta: number, idtipios: number, datumpreseka: Date, idkorisnika: number) {
let bodyString = JSON.stringify({ idtipkomitenta, idtipios, datumpreseka, idkorisnika });
let headers = new HttpHeaders({ 'Content-Type': 'application/JSON' });
return this._http.post<number>('faktura/generisiios', bodyString, { headers });
}
http.post
的第二个参数是 post 请求的主体。只需将两个值都放入正文中,然后在服务器上将它们从正文中取出。
return this.http.post(this.host+"/saveProjectToClient", {
idPerson,
idProject,
}, {
headers:new HttpHeaders({
'Authorization': this.authService.getToken()
})
});
在服务器上(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);
}
}
createRange(range: any, details: any): Observable {
let headers = new HttpHeaders().set('Content-Type', 'application/json');
let body = JSON.stringify({ 'range': range, 'details': details });
return this.httpClient.post(this.serviceURL + '/save', body, { headers });
}