`
deng131
  • 浏览: 661731 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

ajax中application/x-www-form-urlencoded字符编码

阅读更多
在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型。 下边是说明: application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。 multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分。 text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

form的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded。 当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2...),然后把这个字串append到url后面,用?分割,加载这个新的url。 当action为post时候,浏览器把form数据封装到http body中,然后发送到server。 如果没有type=file的控件,用默认的application/x-www-form-urlencoded就可以了。 但是如果有type=file的话,就要用到multipart/form-data了。浏览器会把整个表单以控件为单位分割,并为每个部分加上Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件name)等信息,并加上分割符(boundary)。

引用
The MIME types you mention are the two Content-Type headers for HTTP POST requests that user-agents (browsers) must support. The purpose of both of those types of requests is to send a list of name/value pairs to the server. Depending on the type and amount of data being transmitted, one of the methods will be more efficient than the other. To understand why, you have to look at what each is doing under the covers.

For application/x-www-form-urlencoded, the body of the HTTP message sent to the server is essentially one giant query string -- name/value pairs are separated by the ampersand (&), and names are separated from values by the equal symbal (=). An example of this would be:

MyVariableOne=ValueOne&MyVariableTwo=ValueTwo

According to the specification:

[Reserved and] non-alphanumeric characters are replaced by `%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character
That means that for each non-alphanumeric byte that exists in one of our values, it's going to take three bytes to represent it. For large binary files, tripling the payload is going to be highly inefficient.

That's where multipart/form-data comes in. With this method of transmitting name/value pairs, each pair is represented as a "part" in a MIME message (as described by other answers). Parts are separated by a particular string boundary (chosen specifically so that this boundary string does not occur in any of the "value" payloads). Each part has its own set of MIME headers like Content-Type, and particularly Content-Disposition, which can give each part its "name." The value piece of each name/value pair is the payload of each part of the MIME message. The MIME spec gives us more options when representing the value payload -- we can choose a more efficient encoding of binary data to save bandwidth (e.g. base 64 or even raw binary).

Why not use multipart/form-data all the time? For short alphanumeric values (like most web forms), the overhead of adding all of the MIME headers is going to significantly outweigh any savings from more efficient binary encoding.

The moral of the story is, if you have binary (non-alphanumeric) data (or a significantly sized payload) to transmit, use multipart/form-data. Otherwise, use application/x-www-form-urlencoded.


参考:http://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data
分享到:
评论

相关推荐

    axios针对后台只收form表单形式的post提交

    所以 form 表单提交 和 $.ajax 都是默认 application/x-www-form-urlencoded 请求长这个样子 curl 'http://192.168.11.88:8080/r_server_manager/api/open/user/vo' -H 'Connection: keep-alive' -H 'Prag

    ajax smarty

    form表单的enctype属性:...1、enctype="application/x-www-form-urlencoded"(默认) 说明:代表当前表单只能提交字符信息 2、enctype="multipart/form-data" 说明:代表当前表单不仅能提交字符信息也可以提交字节信息

    ajax中send的用法

    xmlHttpRequest.setRequestHeder("Content-Type","application/x-www-form-urlencoded;charset=UTF-8"); xmlHttpRequest.send("user="+username+"&pwd;="+password); 需要注意的是根据提交方式的不同,两种...

    Ajax传递特殊字符的数据如何解决

    问题描述 如下,对含有特殊字符的... "application/x-www-form-urlencoded"); 换为: req.setRequestHeader(“Content-type”,  “application/json; charset=utf-8”); 后台接受数据: //进行json数据的接收 String

    ajax乱码解决汇总

    第四,ajax发送数据的时候如果修改 Content-Type 为 application/x-www-form-urlencoded",肯定是用post方式,而“太大的数据往往会出错”是用GET方式发送数据造成的。 第五,用vbscript写的函数是用来把数据转成gbk...

    asp.net中在用ajax格式传递数据到aspx页面时出现乱码

    XmlHttp.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”); XmlHttp.send(“QueryName=” + escape(Queryname) + “&QuerySex=” +escape(Querysex)); 在test.aspx中: 代码如下: ...

    jquery.ajax的url中传递中文乱码问题的解决方法

    JQuery默认的contentType:application/x-www-form-urlencoded 这才是JQuery正在乱码的原因,在未指定字符集的时候,是使用ISO-8859-1 ISO8859-1,通常叫做Latin-1。Latin-1包括了书写所有西方欧洲语言不可缺少的...

    jQuery 1.6 API 中文版

    默认值是"application/x-www-form-urlencoded",适合大多数情况。如果你明确地传递了一个content-type给 $.ajax() 那么他必定会发送给服务器(即使没有数据要发送)。数据将总是使用UTF-8字符集传递给服务器;你必须...

    JQuery.ajax传递中文参数的解决方法 推荐

    本人最近也遇到了需要传递中文参数的问题。在网上搜索一下,复制粘贴发的到处都是的“终极”“解决方案”无非... 不同之处在于JQuery默认的contentType:application/x-www-form-urlencoded 而Prototype则是contentType

    真正的JQuery.ajax传递中文参数的解决方法

    本人最近也遇到了需要传递中文参数的问题。在网上搜索一下,复制粘贴发的到处都是的“终极”“解决方案”无非就是...不同之处在于JQuery默认的contentType:application/x-www-form-urlencoded 而Prototype则是conte

    django中使用POST方法获取POST数据

    xmlhttp.setRequestHeader(Content-type,application/x-www-form-urlencoded); 在django的views.py相关方法中,需要通过request.POST获取表单的键值数据,并且可以通过reques.body获取整个表单数据的字符串内容 if...

    jquery电子文档chm

    contentType (String) : (默认: "application/x-www-form-urlencoded") 发送信息至服务器时内容编码类型。默认值适合大多数应用场合。 data (Object,String) : 发送到服务器的数据。将自动转换为请求字符串格式。...

    XML轻松学习手册--XML肯定是未来的发展趋势,不论是网页设计师还是网络程序员,都应该及时学习和了解

    API全称Application Programming Interface,它是访问和操作对象的规则。而DOM就是一种详细描述HTML/XML文档对象规则的API。它规定了HTML/XML文档对象的命名协定,程序模型,沟通规则等。在XML文档中,我们可以将每...

    Ajax课件学习(免费)

    1浏览器的同步 1.1说明 ... 4、如果用post请求向服务器发送数据,需要将”Content-type”的首部设置为”application/x-www-form-urlencoded”.它会告知服务器正在发送数据,并且数据已经符合url编码了。

    jquery-1.1.3 效率提高800%

    数据类型: Boolean 在默认的情况下,如果data选项传进的数据是一个对象而不是字符串,将会自动地被处理和转换成一个查询字符串,以适应默认的content-type--"application/x-www-form-urlencoded"。如果想发送...

    html入门到放弃笔记

    3、指定网页的字符编码格式为 utf-8 4、在 body 中 输出一句话 "我的第一HTML页面" 5、设置 body text为red,bgcolor为yellow 3、文本 1、特殊文本的实现 页面的空格以及一些特殊字符需要通过转义字符的方式...

Global site tag (gtag.js) - Google Analytics