Browse Source

🐛 fix(api): 修复204 No Content响应处理问题

- 添加对204 No Content响应的特殊处理,避免尝试序列化空响应体
- 提取响应体处理逻辑为单独变量,提高代码可读性
- 确保204响应返回null作为body,符合标准Response接口规范
yourname 4 months ago
parent
commit
aff53618ae
1 changed files with 8 additions and 2 deletions
  1. 8 2
      src/client/api.ts

+ 8 - 2
src/client/api.ts

@@ -41,9 +41,15 @@ const axiosFetch = async (url: RequestInfo | URL, init?: RequestInit) => {
   }
     
   
+  // 处理204 No Content响应,不设置body
+  const body = response.status === 204
+    ? null
+    : responseHeaders.get('content-type')?.includes('application/json')
+      ? JSON.stringify(response.data)
+      : response.data;
+  
   return new Response(
-    responseHeaders.get('content-type')?.includes('application/json') ? 
-      JSON.stringify(response.data) : response.data, 
+    body,
     {
       status: response.status,
       statusText: response.statusText,