# 错误：服务器操作需要将请求体解析为 FormData 或 JSON。收到意外的内容类型。

- **ID:** `nextjs/server-action-body-parsed-json-error`
- **领域:** nextjs
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

Next.js 中的服务器操作期望客户端发送带有 Content-Type application/x-www-form-urlencoded 或 multipart/form-data（用于表单提交）或 application/json（用于编程调用）的请求。不支持的内容类型（例如 text/plain）会导致解析失败。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 14.2.0 | active | — | — |
| 14.2.1 | active | — | — |
| 15.0.0 | active | — | — |
| 15.1.0 | active | — | — |

## 解决方案

1. ```
   Ensure client-side form submissions use the standard HTML form element or the 'useFormState' hook, which automatically sends the correct content type. For programmatic calls, use 'new FormData(formElement)' or pass a plain object and set Content-Type: application/json.
   ```
2. ```
   If using fetch directly, construct the body as FormData: const formData = new FormData(); formData.append('key', value); await fetch('/action', { method: 'POST', body: formData });
   ```

## 无效尝试

- **Adding 'Content-Type: multipart/form-data' header manually in JavaScript fetch calls without properly encoding the body** — The server still receives the body as a string, not as multipart parts. The content type must match the actual encoding. (70% 失败率)
- **Setting 'bodyParser: false' in API route configuration for Server Actions** — Server Actions do not use the API route body parser; they have their own parsing logic. Disabling it has no effect. (90% 失败率)
- **Using JSON.stringify on FormData before sending** — FormData is not a plain object; JSON.stringify will return an empty object. The server expects actual FormData entries. (95% 失败率)
