Table of Contents
是什么决定了表单的编码?
表单编码类型
application/x-www-form-urlencoded
注意:
multipart/form-data
text/plain
application/json及其他MIME类型
后记
Home Web Front-end HTML Tutorial 表单提交时编码类型enctype详解_html/css_WEB-ITnose

表单提交时编码类型enctype详解_html/css_WEB-ITnose

Jun 24, 2016 am 11:20 AM

很早以前,当还没有前端这个概念的时候,我在写表单提交完全不去理会表单数据的编码,在 action属性里写好目标URL,剩下的啊交给浏览器吧~但是现在,更多时候我们都采用Ajax方式提交数据,这种原始的方式仅仅被当成优雅降级的产物。

当我们用异步方式提交表单,就需要稍微关注一下表单数据的编码问题了。回想一下,在写回调函数时是不是有根据过请求的 Content-Type写不同业务逻辑的经历,那这个 Content-Type和表单的编码有什么联系吗?有没有在明明前端已经发数据给后端了,后端的小伙伴死活取不到数据的情况?这些纠结的问题背后的原因真是困扰了我好久,今天在篇文章里就要把它们掰扯清楚!

是什么决定了表单的编码?

熟悉表单元素

的小伙伴,对其中的属性 enctype一定不会陌生,就是它规定了对表单提交给服务器时表单数据编码的内容类型(Content Type)。以下引用,摘自 HTML 4.01规范的Form章节:

enctype = content-type [CI]

This attribute specifies the content typeused to submit the form to the server

content type?这不就是我们在回调函数里判断返回数据的类型,并且是在请求头中的那个玩意儿吗?!没错!就是它!根据 HTML 4.01规范的基础数据类型的说明,这个content type 指定了连接资源的属性,同时也是MIME type的那些媒体类型。

表单编码类型

知道了表单编码由 enctype决定的,那么它究竟有多少可选的取值呢?是不是所有的MIME类型它都能用呢?

实际上,根据 HTML5 规范中所叙述的, enctype具有以下三种选项,其中最后一项 text/plain是相比4.01新增的。

  • application/x-www-form-urlencoded

  • multipart/form-data

  • text/plain

application/x-www-form-urlencoded

这是 默认的编码类型,使用该类型时,会将表单数据中 非字母数字的字符转换成转义字符,如"%HH",然后组合成这种形式 key1=value1&key2=value2;所以后端在取数据后, 要进行解码。

注意:

  • 若表单中有文件,则只留文件名;

multipart/form-data

该类型用于 高效传输文件、非ASCII数据和二进制数据,将表单数据逐项地分成不同的部分,用指定的分割符分割每一部分。每一部分都拥有 Content-Disposition头部,指定了该表单项的键名和一些其他信息;并且每一部分都有可选的 Content-Type,不特殊指定就为 text/plain。下面给出一个采用 multipart/form-data编码类型的例子:

Content-Type: multipart/form-data; boundary=AaB03x   --AaB03x   Content-Disposition: form-data; name="submit-name"   Larry   --AaB03x   Content-Disposition: form-data; name="files"; filename="file1.txt"   Content-Type: text/plain   ... contents of file1.txt ...       --AaB03x--
Copy after login

注意:

  • 一般来说, method和 enctype是两个不同的互不影响的属性,但在传文件时, method必须要指定为 POST,否则文件只剩下filename了;

  • 当没有传文件时, enctype会改回默认的 application/x-www-form-urlencoded。

text/plain

按照键值对排列表单数据 key1=value1\r\nkey2=value2,不进行转义。

注意:

  • 若表单中有文件,则只留文件名;

application/json及其他MIME类型

另外,还需要说明表单数据编码类型 application/json,已经被W3C遗弃(详见 HTML JSON Form Submission),建议不要在

中使用了,即使用了如果浏览器不支持,也会替换成 application/x-www-form-urlencoded。

同理,其余的MIME类型,也不支持,均会替换成默认编码 application/x-www-form-urlencoded。

PS:貌似现在浏览器都不支持了,我先用了下面几个浏览器:

  • FF43:Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:43.0) Gecko/20100101 Firefox/43.0

  • Chrome49, Safari9.1:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36

明天去实验室试试IE~

后记

所以, enctype可以认为就是表单数据的 content type(MIME type),只不过其取值不能用除了上面提到的三个,否则会转换成默认的编码。

今天掰扯完了表单提交时的编码类型 enctype,以及它和 content type、 MIME type的关系。下次再总结一下Ajax提交表单的类型吧。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

What is the difference between <strong>, <b> tags and <em>, <i> tags? What is the difference between <strong>, <b> tags and <em>, <i> tags? Apr 28, 2025 pm 05:42 PM

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

See all articles