我有一个错误,我不知道为什么,但是 Angular 没有在 Spring Boot 中激活我的 api,而且我没有找到错误。
这是代码。
服务( Angular )
setPost(postId:number):Observable<boolean>{
return this.http.post<boolean>(this.myAppUrl+this.myApiPostUrl+postId+'/setPost',this.authService.getLoggedUserFromSessionStorage())
.pipe(
retry(1),
catchError(this.errorHandler)
);
}
组件( Angular )
isAlreadyOpen(){
this.postService.isOpen(this.postId).subscribe( isAuth => {
if(isAuth) {
this.cantModify();
} else {
console.log('im here');
this.postService.setPost(this.postId);
this.router.navigate(['/post/edit/', this.postId]);
}
});
}
代码在 else 中输入没有问题,因为 chrome 控制台打印“im here”。
Controller (JAVA)
@RequestMapping(value = "/posts/{id}/setPost", method = RequestMethod.POST)
public ResponseEntity<Boolean> setPost(@PathVariable("id") int idPost, @RequestBody UserDTO userCorrente) {
PostUser postUser = postUserService.set(idPost, userCorrente);
if (postUser != null) {
return new ResponseEntity<>(true, HttpStatus.OK);
} else {
return new ResponseEntity<>(false, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Angular
this.postService.isOpen
(这个api,工作完美)
不工作this.postService.setPost
,我尝试删除所有内容以查看 map 是否像那样
isAlreadyOpen(){
this.postService.setPost(this.postId);
this.router.navigate(['/post/edit/', this.postId]);
}
}
但是什么都没有,不起作用,我在Chrome的DEV TOOLS(网络)中没有看到请求,并且在Spring控制台中没有看到任何内容。
最佳答案
您需要对 this.postService.setPost(this.postId)
调用 subscribe()
,否则不会触发请求。
关于java - Angular 的请求不要在后端映射我的 API(Spring Boot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58766231/