From f6d52b90f004ad1a66ee63009f2aa549ee8eaae1 Mon Sep 17 00:00:00 2001 From: Mingxiangyu <49153313+Mingxiangyu@users.noreply.github.com> Date: Tue, 28 Oct 2025 14:16:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E4=B8=BB=E6=9C=BA=E5=9C=B0=E5=9D=80=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=B1=80=E5=9F=9F=E7=BD=91=E5=86=85=E5=90=8E=E7=AB=AF?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Updated iframe source URLs to use the window's hostname instead of localhost.本次提交解决了局域网内其他设备无法正常访问后端服务的问题。此前前端代码中硬编码了localhost作为后端服务的主机地址,导致其他设备访问时,localhost会指向设备自身而非实际运行服务的主机,从而无法连接后端。 主要修改内容: 将前端中硬编码的localhost替换为window.location.hostname,动态获取当前页面的主机 IP(即运行服务的电脑局域网 IP)。 涉及修改的关键位置: preloadIframes函数:预加载 iframe 时,使用动态主机地址生成后端服务 URL(如http://${window.location.hostname}:8501)。 performSearch函数:发送搜索请求时,同样使用动态主机地址构建请求 URL,确保后端接口调用正确指向服务主机。 注意事项: 为确保局域网访问正常,后端服务(运行在 8501、8502、8503 等端口)需配置为绑定到0.0.0.0(而非默认的localhost),以允许来自局域网的连接。例如,Python 服务可通过app.run(host='0.0.0.0', port=8501)启动。 效果: 修改后,局域网内其他设备通过服务主机的局域网 IP(如192.168.1.100)访问前端页面时,可自动正确连接到后端服务,实现跨设备正常使用。 --- templates/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/index.html b/templates/index.html index 4cef46a..0eda843 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1002,7 +1002,7 @@ totalRunning++; // 构建搜索URL - const searchUrl = `http://localhost:${ports[app]}?query=${encodeURIComponent(query)}&auto_search=true`; + const searchUrl = `http://${window.location.hostname}:${ports[app]}?query=${encodeURIComponent(query)}&auto_search=true`; console.log(`向 ${app} 发送搜索请求: ${searchUrl}`); // 直接更新主iframe的src来传递搜索参数 @@ -1206,7 +1206,7 @@ for (const [app, port] of Object.entries(ports)) { const iframe = document.createElement('iframe'); - iframe.src = `http://localhost:${port}`; + iframe.src = `http://${window.location.hostname}:${port}`; iframe.style.width = '100%'; iframe.style.height = '100%'; iframe.style.border = 'none';