typescript Angular 6 http.delete 请求不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52244651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 6 http.delete request not working
提问by Heiko Piirme
I cant seem to get my delete request to work. I have finished all of the get requests but now I'm stuck on delete and can't seem to wrap my head around it.
我似乎无法让我的删除请求生效。我已经完成了所有的获取请求,但现在我被困在删除并且似乎无法解决它。
The console.log'd URL is always correct and the delete request works fine via Postman.
console.log 的 URL 总是正确的,删除请求通过 Postman 工作正常。
Got any ideas?
有什么想法吗?
HTML
HTML
<button class="button button3" (click)="delTicket()"><span class="fa fa-trash"></span></button>
TS
TS
delTicket(){
this.id = this.route.snapshot.params.id;
this.ticketService.deleteTicket(this.id);
}
Service
服务
deleteTicket(id): Observable<Ticket[]>{
console.log(this.apiUrl + id);
return this.http.delete<Ticket[]>(this.apiUrl + id);
}
回答by Sajeetharan
You need to call subscribe()inside your component, otherwise request wont get invoked
您需要在组件内部调用subscribe(),否则不会调用请求
delTicket(){
this.id = this.route.snapshot.params.id;
this.ticketService.deleteTicket(this.id).subscribe((data)=>{
console.log("success");
});
}
回答by Vishal SIngh
You must call subscribe()or nothing happens. Just calling ticketService.deleteTicket(this.id) does not initiate the DELETE request.
您必须调用subscribe()否则什么也不会发生。只是调用 ticketService.deleteTicket(this.id) 不会发起 DELETE 请求。
- An HttpClient method does not begin its HTTP request until you call subscribe() on the observable returned by that method. This is true for all HttpClient methods.
- Calling subscribe(...) triggers execution of the observable and causes HttpClient to compose and send the HTTP request to the server.
- HttpClient 方法不会开始其 HTTP 请求,直到您对该方法返回的 observable 调用 subscribe() 。这适用于所有 HttpClient 方法。
- 调用 subscribe(...) 会触发 observable 的执行,并导致 HttpClient 组合并将 HTTP 请求发送到服务器。