Angular,如何检查Post是否完成。 ToPromise() 和 subscribe() 不工作
Angular, how to check if Post is done. ToPromise() and subscribe() not working
我不知道为什么(这是我第一次尝试),但即使我的 post 请求有效,我也无法在完成后打印任何内容。
在 TS 文件中,这是我的代码:
this.appService.sendPath({ title: serviceToPath }).toPromise().then
(
()=>
{
console.log("The path is set!")
}
);
那么这是我的中间人“appService”
sendPath(path: any): Observable<any> {
console.log("path from app-service: "+ JSON.stringify(path))
return this.httpClient.post('http://localhost:3000/compare',path)
}
当然是控制台消息“路径已设置!”当“来自应用程序服务的路径”完美运行时,从不显示在控制台上。
最终目标是在 POST 完成它的事情后调用一个函数,但现在,我只想在控制台上打印一些东西。
感谢您的帮助。
更新:
这似乎是因为我的 Post 请求处于永远挂起的状态。我该如何解决这个问题?
为什么不直接订阅您的功能?
this.appService.sendPath({ title: serviceToPath })
.subscribe(res => {
console.log("The path is set!")
});
确实是服务器端出了问题。再次感谢 IAfanasov 给了我很大的线索!
//If you forget this the promise will not work and you will never know when POST is over
res.status(205).send('');
我没有发送状态,所以前端承诺不知道什么时候结束。
我不知道为什么(这是我第一次尝试),但即使我的 post 请求有效,我也无法在完成后打印任何内容。
在 TS 文件中,这是我的代码:
this.appService.sendPath({ title: serviceToPath }).toPromise().then
(
()=>
{
console.log("The path is set!")
}
);
那么这是我的中间人“appService”
sendPath(path: any): Observable<any> {
console.log("path from app-service: "+ JSON.stringify(path))
return this.httpClient.post('http://localhost:3000/compare',path)
}
当然是控制台消息“路径已设置!”当“来自应用程序服务的路径”完美运行时,从不显示在控制台上。
最终目标是在 POST 完成它的事情后调用一个函数,但现在,我只想在控制台上打印一些东西。
感谢您的帮助。
更新: 这似乎是因为我的 Post 请求处于永远挂起的状态。我该如何解决这个问题?
为什么不直接订阅您的功能?
this.appService.sendPath({ title: serviceToPath })
.subscribe(res => {
console.log("The path is set!")
});
确实是服务器端出了问题。再次感谢 IAfanasov 给了我很大的线索!
//If you forget this the promise will not work and you will never know when POST is over
res.status(205).send('');
我没有发送状态,所以前端承诺不知道什么时候结束。