# 错误：服务器操作解析 FormData 失败：数据意外结束

- **ID:** `nextjs/server-action-formdata-parse-fail`
- **领域:** nextjs
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

发送到服务器操作的 FormData 不完整或格式错误，通常是由于客户端 fetch 或表单提交在发送前截断了请求体。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| next@14.2.0 | active | — | — |
| next@14.2.5 | active | — | — |
| next@15.0.0-rc.1 | active | — | — |

## 解决方案

1. ```
   确保客户端表单提交正确使用原生 FormData API。对于 fetch，避免手动设置头部：`const formData = new FormData(); formData.append('field', value); await fetch('/api/action', { method: 'POST', body: formData });`
   ```
2. ```
   在发送前验证 FormData，通过日志或检查条目：`for (let [key, value] of formData.entries()) { console.log(key, value); }` 确认所有字段都存在。
   ```
3. ```
   如果使用自定义 fetch，确保没有过早关闭流。使用 `await fetch(url, { method: 'POST', body: formData })`，不要用 JSON.stringify 包裹。
   ```

## 无效尝试

- **Adding 'Content-Type: multipart/form-data' header manually in fetch calls** — The browser automatically sets the correct Content-Type with boundary for FormData; manually setting it can break parsing. (70% 失败率)
- **Increasing server body parser size limit in next.config.js** — The error is not about size limits but about incomplete data transmission, often due to client-side logic errors. (50% 失败率)
- **Switching to JSON.stringify instead of FormData for the request body** — Server Actions expect FormData for 'use server' functions; sending JSON requires different handling and may cause type mismatches. (60% 失败率)
