Long ago, when the front end of the concept yet, and I'm writing completely ignore the form is submitted form data encoded in the action
written target URL property, the rest of it ~ ah to the browser but now, more Ajax way we are used to submit data, this primitive way is just as graceful degradation products.
When we submit the form asynchronous mode, you need to look a little coding problems of the form data. Recall that at the time of writing the callback function is not there been a request based on the Content-Type
different business logic of writing experience, then this Content-Type
and coding forms have any contact you? There is no clearly been made in the front-end data to the back end, and the rear end of a small partner to take life and death situation than data? The reason behind these tangled problem is really bothering me for a long time, today put them in articles in breaking pull clear!
What determines the form of coding?
Familiar form elements <form>
of a small partner, the properties of which enctype
will not be unfamiliar, is that it provides for a form submitted to the server when the content type (Content Type) data encoded form. The following quote, taken from HTML 4.01 Form chapters specifications :
enctype = content-type [CI]
This attribute specifies the content type used to submit the form to the server
content type? Is not that what we judge the return type of the data in the callback function, and that stuff is in the request header do? ! Yes! that's it! According to HTML 4.01 specification of the underlying data type description of this content type specifies the properties of the connection resources , but also those MIME type of media types.
Form encoding type
Know the form encoded by enctype
the decision, then it is how much value there is an optional it? MIME type is not all it can to use it?
In fact, according to the HTML5 specification as described, enctype
it has the following three options, the last of which text/plain
is new compared to 4.01.
application/x-www-form-urlencoded
multipart/form-data
text/plain
application/x-www-form-urlencoded
This is the default encoding type , the use of this type, the data will form non-alphanumeric characters into an escape character , such as "% HH", then combined into this form key1=value1&key2=value2
; after taking it in the rear end of the data, to decode .
note:
If there are files in the form, leaving only the file name;
multipart/form-data
This type is used for efficient file transfer, binary data and non-ASCII data , item by item, the form data into different portions, each portion divided by the specified delimiters. Each section has a Content-Disposition
header that specifies the form item name and other key information; and each part has an optional Content-Type
, it is no designated special text/plain
. The following presents a use multipart/form-data
example of encoding types:
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--
note:
In general,
method
andenctype
are two different properties independently of each other, but at the time of transfer files,method
must be specified asPOST
, otherwise, only the filename of the file;When there is no file transfer,
enctype
it will be changed back to the defaultapplication/x-www-form-urlencoded
.
text/plain
Key arrangement according to the form data key1=value1\r\nkey2=value2
, without escaping.
note:
If there are files in the form, leaving only the file name;
application / json and other MIME types
In addition, also should be noted that the form data encoding type application/json
, W3C has been abandoned (see HTML JSON Form Submission ), it is not recommended <form enctype="...">
use, that is used if the browser does not support, will be replaced application/x-www-form-urlencoded
.
Similarly, the rest of the MIME type does not support, will replace the default encoding application/x-www-form-urlencoded
.
PS: I do not support the browser looks like now, and I use the following several browsers:
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
IE6 8
postscript
So, enctype
you can think of is the form data content type(MIME type)
, but its value can not be used in addition to the above-mentioned three, otherwise it will be converted into the default encoding.
Today, breaking pull over encoding type when the form is submitted enctype
, and it content type
, MIME type
of relationships. Ajax next time to sum up the type of form is submitted it.