nextjs
runtime_error
ai_generated
true
错误:服务器操作解析 FormData 失败:数据意外结束
Error: Server Action failed to parse FormData: unexpected end of data
ID: nextjs/server-action-formdata-parse-fail
82%修复率
85%置信度
1证据数
2024-06-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| [email protected] | active | — | — | — |
| [email protected] | active | — | — | — |
| [email protected] | active | — | — | — |
根因分析
发送到服务器操作的 FormData 不完整或格式错误,通常是由于客户端 fetch 或表单提交在发送前截断了请求体。
English
The FormData sent to a Server Action is incomplete or malformed, often due to a client-side fetch or form submission that truncates the body before sending.
官方文档
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#formdata解决方案
-
确保客户端表单提交正确使用原生 FormData API。对于 fetch,避免手动设置头部:`const formData = new FormData(); formData.append('field', value); await fetch('/api/action', { method: 'POST', body: formData });` -
在发送前验证 FormData,通过日志或检查条目:`for (let [key, value] of formData.entries()) { console.log(key, value); }` 确认所有字段都存在。 -
如果使用自定义 fetch,确保没有过早关闭流。使用 `await fetch(url, { method: 'POST', body: formData })`,不要用 JSON.stringify 包裹。
无效尝试
常见但无效的做法:
-
Adding 'Content-Type: multipart/form-data' header manually in fetch calls
70% 失败
The browser automatically sets the correct Content-Type with boundary for FormData; manually setting it can break parsing.
-
Increasing server body parser size limit in next.config.js
50% 失败
The error is not about size limits but about incomplete data transmission, often due to client-side logic errors.
-
Switching to JSON.stringify instead of FormData for the request body
60% 失败
Server Actions expect FormData for 'use server' functions; sending JSON requires different handling and may cause type mismatches.