@HostListener监听window:beforeunload时HTTP请求在HTTPS环境下偶发失效的问题求助
@HostListener监听window:beforeunload时HTTP请求在HTTPS环境下偶发失效的问题求助
大家好,我最近在Angular项目里遇到了一个头疼的问题,想请社区的朋友们帮忙分析下:
我通过@HostListener监听window:beforeunload事件,希望在用户刷新页面、关闭浏览器标签页或窗口时,自动调用接口关闭当前的聊天会话。核心实现代码如下:
@HostListener('window:beforeunload', ['$event']) handleBeforeUnload(event: Event) { console.log("chat session id ", this.sessionService.getSessionId()) if (this.sessionService.getSessionId() != null) { this.chatService.closeAgent(); this.sessionService.removeSessionId() } } // chatService中的closeAgent方法 closeAgent() { if((environment.genAIService === 'IG_GENAI_SERVICE')&&(this.sessionService.getSessionId()!=null)){ const chatSessionId = this.sessionService.getSessionId(); const url = `va/close-chat/${chatSessionId}`; this.http.get<boolean>(url).subscribe({ next: (response) => { console.log('API call successfully ',response); //this.tosterService.showInfo("Your session is closed") } }); } }
这段代码在本地HTTP环境下运行完全正常,每次触发页面卸载事件时,接口都会被成功调用,会话也能正常关闭。但部署到HTTPS生产环境后,这个请求偶尔会失效——看起来接口根本没被触发,或者请求没有成功到达后端。
我已经排查了跨域配置、后端接口本身的可用性,都没找到问题根源。有没有朋友遇到过类似的情况?或者能给我一些排查方向和解决方案的建议?
内容来源于stack exchange
相关产品推荐
相关产品推荐

