From f06d6e5a7e88358494632348690bf6ae505f0ed7 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Sun, 1 Feb 2026 22:07:43 +0800 Subject: [PATCH] =?UTF-8?q?fix(utils):=20=E4=BF=AE=E5=A4=8D=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=B7=A5=E5=85=B7=E4=B8=AD=E7=9A=84XMLHttpRequest?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移动XMLHttpRequest实例化到方法开头避免重复创建 - 删除被注释掉的旧请求完成事件处理代码 - 修正请求错误和中止事件的错误处理逻辑 - 移除重复的xhr.open调用确保正确的HTTP方法设置 --- frontend/src/utils/request.ts | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/frontend/src/utils/request.ts b/frontend/src/utils/request.ts index b3d972b..aed6743 100644 --- a/frontend/src/utils/request.ts +++ b/frontend/src/utils/request.ts @@ -82,6 +82,9 @@ class Request { */ createXHRWithProgress(url, config, onProgress, onDownloadProgress) { return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.open(config.method || "POST", url); + // 设置请求头 if (config.headers) { Object.keys(config.headers).forEach((key) => { @@ -89,8 +92,6 @@ class Request { }); } - const xhr = new XMLHttpRequest(); - // 监听上传进度 xhr.upload.addEventListener("progress", function (event) { if (event.lengthComputable) { @@ -103,14 +104,6 @@ class Request { } }); - // 请求完成 - // xhr.addEventListener("load", function () { - // if (xhr.status >= 200 && xhr.status < 300) { - // const response = JSON.parse(xhr.responseText); - // resolve(xhr); - // } - // }); - // 请求完成处理 xhr.addEventListener("load", () => { if (xhr.status >= 200 && xhr.status < 300) { @@ -142,16 +135,15 @@ class Request { // 请求错误 xhr.addEventListener("error", function () { console.error("网络错误"); - if (onError) onError(new Error("网络错误")); + reject(new Error("网络错误")); }); // 请求中止 xhr.addEventListener("abort", function () { console.log("上传已取消"); - if (onError) onError(new Error("上传已取消")); + reject(new Error("上传已取消")); }); - xhr.open("POST", url); xhr.send(config.body); return xhr; // 返回 xhr 对象以便后续控制