惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Palo Alto Networks Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
S
Schneier on Security
S
Securelist
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cyberwarzone
Cyberwarzone
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
GbyAI
GbyAI
Security Latest
Security Latest
Last Week in AI
Last Week in AI
Microsoft Security Blog
Microsoft Security Blog
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
C
Cisco Blogs
博客园 - 【当耐特】
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
B
Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Attack and Defense Labs
Attack and Defense Labs
The Last Watchdog
The Last Watchdog
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
Project Zero
Project Zero
WordPress大学
WordPress大学
L
LINUX DO - 最新话题
F
Fortinet All Blogs
L
LINUX DO - 热门话题
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
MongoDB | Blog
MongoDB | Blog
Latest news
Latest news
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
爱范儿
爱范儿
O
OpenAI News
J
Java Code Geeks
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - Albert_MIN

在 ASP.NET Core MVC 中,接收数据的几种方式 Revit二次开发 钢筋生成API(二) Revit二次开发 钢筋生成API(一) revit二次开发 钢筋布置方式 revit二次开发之 钢筋功能详细分析 revit 二次开发之收集器、过滤器和选择器 Revit Server的注意要配置说明 WCF文件配置服务 WCF服务的各种绑定 Revit二次开发之 对象的隐藏与显示 Revit二次开发之 GeometryObject分析 Revit二次开发之 Material 分析 Revit二次开发之 PolymeshTopology Revit 二次开发之 图纸的导出 Revit开发之 IExportContext接口详细 JavaScript 困惑之 ArrayBuffer JS 困惑之this的指向 Revit二次开发之 族的创建 Revit二次开发 钢筋生成 Revit二次开发之 尺寸标线(二) Revit二次开发之 尺寸标线
Content-Type 对应不同的前端数据结构与构造方式
Albert_MIN · 2026-07-03 · via 博客园 - Albert_MIN

你提到的三种 Content-Type 对应不同的前端数据结构与构造方式,以下是详细说明和示例:


一、文件上传 → multipart/form-data

适用场景:上传图片、视频、附件等二进制文件

前端核心:使用 FormData对象自动处理边界和编码

数据结构示例(JavaScript)

// 假设有一个 <input type="file" id="fileInput">
const file = document.getElementById('fileInput').files[0];
const formData = new FormData();

关键点

  • 无需手动设置Content-Type,浏览器会自动生成带 boundary的头部

  • 支持混合数据类型(文件 + 普通字段)

  • 后端通常用 multer(Node.js)、MultipartResolver(Spring)等解析


二、纯表单无文件 → application/x-www-form-urlencoded

适用场景:传统 HTML 表单提交、简单键值对数据

前端核心:键值对 URL 编码(key=value&key2=value2

数据结构示例

方式 1:URLSearchParams(推荐)

const params = new URLSearchParams();
params.append('username', '张三');
params.append('age', '25');
params.append('hobby', '篮球'); 

方式 2:手动拼接字符串

const data = 'username=张三&age=25&hobby=' + encodeURIComponent('篮球');

fetch('/api/login', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: data,
});

限制

  • 不支持嵌套对象(如 { user: { name: '张三' } }

  • 特殊字符需编码(encodeURIComponent

  • 数据量有限制(URL 长度限制)


三、前后端分离 API → application/json

适用场景:RESTful API、复杂数据结构、SPA 应用

前端核心:JSON 序列化

数据结构示例

const product = {
  id: 'P001',
  name: '智能手机',
  specs: {
    color: '黑色',
    storage: '256GB',
  },
  tags: ['数码', '5G'],
};

fetch('/api/products', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer token123', 

优势

  • 支持任意复杂嵌套结构

  • 前后端数据格式统一(JavaScript 对象 ↔ JSON)

  • 便于接口文档定义(OpenAPI/Swagger)


四、三种方式对比总结

场景

Content-Type

前端数据结构

是否支持文件

复杂度

文件上传

multipart/form-data

FormData对象

✅ 支持

纯表单

application/x-www-form-urlencoded

键值对字符串

❌ 不支持

API 接口

application/json

JavaScript 对象

❌ 不直接支持


五、实际开发建议

  1. 文件上传:始终用 FormData,不要尝试用 JSON 传文件(Base64 除外,但效率低)

  2. 表单提交:简单表单用 x-www-form-urlencoded,复杂表单可考虑 JSON

  3. API 设计:优先使用 JSON,保持前后端一致

  4. 注意跨域application/json会触发 CORS 预检请求(OPTIONS)