Html 在 Internet Explorer 的表单提交中设置字符编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/153527/
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-28 22:26:14  来源:igfitidea点击:

Setting the character encoding in form submit for Internet Explorer

htmlinternet-explorerformsencodingiso-8859-1

提问by Juha Syrj?l?

I have a page that contains a form. This page is served with content type text/html;charset=utf-8. I need to submit this form to server using ISO-8859-1 character encoding. Is this possible with Internet Explorer?

我有一个包含表单的页面。此页面的内容类型为 text/html;charset=utf-8。我需要使用 ISO-8859-1 字符编码将此表单提交给服务器。这可能与 Internet Explorer 一起使用吗?

Setting accept-charset attribute to form element, like this, works for Firefox, Opera etc. but not for IE.

将 accept-charset 属性设置为表单元素,像这样,适用于 Firefox、Opera 等,但不适用于 IE。

<form accept-charset="ISO-8859-1">
  ...
</form>

Edit: This form is created by server A and will be submitted to server B. I have no control over server B.

编辑:此表单由服务器 A 创建,并将提交给服务器 B。我无法控制服务器 B。

If I set server A to serve content with charset ISO-8859-1 everything works, but I am looking a way to make this work without changes to server A's encoding. I have another question about setting the encoding in server A.

如果我将服务器 A 设置为使用字符集 ISO-8859-1 提供内容,则一切正常,但我正在寻找一种无需更改服务器 A 编码即可使其工作的方法。我还有一个关于在服务器 A 中设置编码的问题

回答by Juha Syrj?l?

There is a simple hack to this:

对此有一个简单的技巧:

Insert a hidden input field in the form with an entity which only occur in the character set the server your posting (or doing a GET) to accepts.

在表单中插入一个隐藏的输入字段,其中包含一个实体,该实体仅出现在您发布(或执行 GET)的服务器接受的字符集中。

Example: If the form is located on a server serving ISO-8859-1 and the form will post to a server expecting UTF-8 insert something like this in the form:

示例:如果表单位于提供 ISO-8859-1 服务的服务器上,并且表单将发布到需要 UTF-8 的服务器,请在表单中插入如下内容:

<input name="iehack" type="hidden" value="&#9760;" />

IE will then "detect" that the form contains a UTF-8 character and use UTF-8 when you POST or GET. Strange, but it does work.

然后,IE 将“检测”表单包含 UTF-8 字符并在您 POST 或 GET 时使用 UTF-8。奇怪,但它确实有效。

回答by dgaspar

With decent browsers:

使用不错的浏览器:

<form accept-charset="ISO-8859-1" .... >

With IE (any):

使用 IE(任何):

document.charset = 'ISO-8859-1'; // do this before submitting your non-utf8 <form>!

回答by Juha Syrj?l?

It seems that this can't be done, not at least with current versions of IE (6 and 7).

这似乎无法完成,至少对于当前版本的 IE(6 和 7)而言是这样。

IE supports form attribute accept-charset, but only if its value is 'utf-8'.

IE 支持表单属性 accept-charset,但前提是其值为 'utf-8'。

The solution is to modify server A to produce encoding 'ISO-8859-1' for page that contains the form.

解决方案是修改服务器 A 为包含表单的页面生成编码“ISO-8859-1”。

回答by Christian

I've got the same problem here. I have an UTF-8 Page an need to post to an ISO-8859-1 server.

我这里也有同样的问题。我有一个 UTF-8 页面,需要发布到 ISO-8859-1 服务器。

Looks like IE can't handle ISO-8859-1. Butit can handle ISO-8859-15.

看起来 IE 无法处理 ISO-8859-1。但是,它可以处理ISO-8859- 15

<form accept-charset="ISO-8859-15">
  ...
</form>

So this worked for me, since ISO-8859-1 and ISO-8859-15 are almost the same.

所以这对我有用,因为 ISO-8859-1 和 ISO-8859-15 几乎相同。

回答by Edward Z. Yang

If you have any access to the server at all, convert its processing to UTF-8. The art of submitting non-UTF-8 forms is a long and sorry story; this document about forms and i18nmay be of interest. I understand you do not seem to care about international support; you can always convert the UTF-8 data to html entities to make sure it stays Latin-1.

如果您完全可以访问服务器,请将其处理转换为 UTF-8。提交非 UTF-8 表单的艺术是一个漫长而遗憾的故事;这份关于表格和 i18n 的文档可能会引起兴趣。我知道您似乎并不关心国际支持;您始终可以将 UTF-8 数据转换为 html 实体,以确保它保持 Latin-1。

回答by dr.dimitru

For Russian symbols 'windows-1251'

对于俄语符号“windows-1251”

<form action="yourProcessPage.php" method="POST" accept-charset="utf-8">
<input name="string" value="string" />
...
</form>

When simply convert string to cp1251

当简单地将字符串转换为 cp1251

$string = $_POST['string'];
$string = mb_convert_encoding($string, "CP1251", "UTF-8");

回答by David Mongeau-Petitpas

Just got the same problem and I have a relatively simple solution that does not require any change in the page character encoding(wich is a pain in the ass).

刚遇到同样的问题,我有一个相对简单的解决方案,不需要对页面字符编码进行任何更改(这真是太麻烦了)。

For example, your site is in utf-8 and you want to post a form to a site in iso-8859-1. Just change the action of the post to a page on your site that will convert the posted values from utf-8 to iso-8859-1.

例如,您的站点是 utf-8 格式,而您想将表单发布到 iso-8859-1 格式的站点。只需将帖子的操作更改为您网站上的页面,该页面会将发布的值从 utf-8 转换为 iso-8859-1。

this could be done easily in php with something like this:

这可以在 php 中轻松完成,如下所示:

<?php
$params = array();
foreach($_POST as $key=>$value) {
    $params[] = $key."=".rawurlencode(utf8_decode($value));
}
$params = implode("&",$params);

//then you redirect to the final page in iso-8859-1
?>

回答by PhiLho

Looks like Microsoft knows accept-charset, but their doc doesn't tell for which version it starts to work...
You don't tell either in which versions of browser you tested it.

看起来微软知道accept-charset,但他们的文档没有说明它开始工作的版本......
你不会告诉你在哪个版本的浏览器中测试它。

回答by warp

I seem to remember that Internet Explorer gets confused if the accept-charset encoding doesn't match the encoding specified in the content-type header. In your example, you claim the document is sent as UTF-8, but want form submits in ISO-8859-1. Try matching those and see if that solves your problem.

我似乎记得如果接受字符集编码与内容类型标头中指定的编码不匹配,Internet Explorer 会感到困惑。在您的示例中,您声称文档以 UTF-8 格式发送,但希望表单以 ISO-8859-1 格式提交。尝试匹配这些,看看是否能解决您的问题。

回答by pdc

I am pretty sure it won't be possible with older versions of IE. Before the accept-charsetattribute was devised, there was no way for formelements to specify which character encoding they accepted, and the best that browsers could do is assume the encoding of the page the form is in will do.

我很确定旧版本的 IE 是不可能的。在accept-charset设计该属性之前,form元素无法指定它们接受哪种字符编码,浏览器所能做的最好的事情就是假设表单所在页面的编码会这样做。

It is a bit sad that you need to know which encoding was used -- nowadays we would expect our web frameworks to take care of such details invisibly and expose the text data to the application as Unicode strings, already decoded...

令人遗憾的是,您需要知道使用了哪种编码——如今,我们希望我们的 Web 框架能够不可见地处理这些细节,并将文本数据作为 Unicode 字符串公开给应用程序,已经解码......