From a6f99e0c49e5233f5cb2d20a42cafc76bd480387 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BB=9D=E5=B0=98?= <237809796@qq.com> Date: Mon, 16 Feb 2026 14:22:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=9C=8D=E5=8A=A1=E5=99=A8=20CORS=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题: - 前端运行在 http://wails.localhost - 文件服务器运行在 http://localhost:18765 - 不同源导致 CORS 错误 修复: - asset_handler.go 添加 CORS 响应头 - 支持 OPTIONS 预检请求 - 允许所有源访问(本地文件服务器) --- internal/filesystem/asset_handler.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/internal/filesystem/asset_handler.go b/internal/filesystem/asset_handler.go index 911900a..35a2fc5 100644 --- a/internal/filesystem/asset_handler.go +++ b/internal/filesystem/asset_handler.go @@ -67,6 +67,17 @@ func StartLocalFileServer() (string, error) { // handleLocalFileRequest 处理本地文件请求 func handleLocalFileRequest(w http.ResponseWriter, r *http.Request) { + // CORS 头:允许所有源访问(因为这是本地文件服务器) + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS") + w.Header().Set("Access-Control-Allow-Headers", "*") + + // 处理 OPTIONS 预检请求 + if r.Method == http.MethodOptions { + w.WriteHeader(http.StatusOK) + return + } + // 只处理 GET 请求 if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)