Html accept-charset="UTF-8" 参数在表单中使用时没有任何作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12830546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:18:25  来源:igfitidea点击:

accept-charset="UTF-8" parameter doesnt do anything, when used in form

htmlformsspring-mvcencodingfreemarker

提问by insomiac

I am using accept-charset="utf-8" attribute in form and found that the when do a form post with non-ascii, the headers have different accept charset option in the request header. Is there anything i am missing ? My form looks like this

我在表单中使用 accept-charset="utf-8" 属性,发现当使用非 ascii 进行表单发布时,请求头中有不同的接受字符集选项。有什么我想念的吗?我的表格看起来像这样

<form method="post" action="controller" accept-charset="UTF-8">
..input text box
.. submit button
</form>

Thanks in advance

提前致谢

回答by Jukka K. Korpela

The question, as asked, is self-contradictory: the heading says that the accept-charsetparameter does not do anything, whereas the question body says that when the accept-charsetattribute (this is the correct term) is used, “the headers have different accept charset option in the request header”. I suppose a negation is missing from the latter statement.

正如所问的,这个问题是自相矛盾的:标题说accept-charset参数不做任何事情,而问题正文说当使用accept-charset属性(这是正确的术语)时,“标题有不同的接受字符集选项请求头”。我想后一个陈述中缺少否定。

Browsers send Accept-Charsetparameters in HTTP request headers according to their own principles and settings. For example, my Chrome sends Accept-Charset:windows-1252,utf-8;q=0.7,*;q=0.3. Such a header is typically ignored by server-side software, but it could be used (and it was designed to be used) to determine which encoding is to be used in the server response, in case the server-side software (a form handler, in this case) is capable of using different encodings in the response.

浏览器Accept-Charset根据自己的原则和设置在 HTTP 请求头中发送参数。例如,我的 Chrome 发送Accept-Charset:windows-1252,utf-8;q=0.7,*;q=0.3. 这样的标头通常会被服务器端软件忽略,但它可以用于(并且它被设计用于)确定在服务器响应中使用哪种编码,以防服务器端软件(表单处理程序) ,在这种情况下)能够在响应中使用不同的编码。

The accept-charsetattribute in a formelement is not expected to affect HTTP request headers, and it does not. It is meant to specify the character encoding to be used for the form data in the request, and this is what it actually does. The HTML 4.01 spec is obscureabout this, but the W3C HTML5 draft puts itmuch better, though for some odd reason uses plural: “gives the character encodings that are to be used for the submission”. I suppose the reason is that you could specify alternate encodings, to prepare for situations where a browser is unable to use your preferred encoding. And what actually happens in Chrome for example is that if you use accept-charset="foobar utt-8", then UTF-8 used.

元素中的accept-charset属性form不会影响 HTTP 请求标头,它也不会。它旨在指定用于请求中表单数据的字符编码,这就是它实际所做的。在HTML 4.01规范是模糊的这一点,但W3C HTML5草案放它要好得多,虽然一些奇怪的原因使用复数:“给人的字符编码将被用于提交”。我想原因是您可以指定备用编码,以应对浏览器无法使用您首选编码的情况。例如,Chrome 中实际发生的情况是,如果您使用accept-charset="foobar utt-8",则使用UTF-8。

In practice, the attribute is used to make the encoding of data submission different from the encoding of the page containing the form. Suppose your page is ISO-8859-1 encoded and someone types Greek or Hebrew letters into your form. Browsers will have to do some error recovery, since those characters cannot be represented in ISO-8859-1. (In practice they turn the characters to numeric character references, which is logically all wrong but pragmatically perhaps the best they can do.) Using <form charset=utf-8>helps here: no matter what the encoding is, the form data will be sent as UTF-8 encoding, which can handle any character.

在实践中,该属性用于使数据提交的编码与包含表单的页面的编码不同。假设您的页面采用 ISO-8859-1 编码,并且有人在您的表单中键入希腊或希伯来字母。浏览器将不得不做一些错误恢复,因为这些字符不能在 ISO-8859-1 中表示。(在实践中,他们将字符转换为数字字符引用,这在逻辑上是错误的,但在务实上也许是他们能做的最好的。)<form charset=utf-8>在这里使用帮助:无论编码是什么,表单数据都将作为 UTF-8 编码发送,可以处理任何字符。

If you wish to tell the form handler which encoding it should use in its response, then you can add a hidden (or non-hidden) field into the form for that.

如果您希望告诉表单处理程序它应该在其响应中使用哪种编码,那么您可以为此在表单中添加一个隐藏(或非隐藏)字段。