如何设置“Content-Type ...字符集”在请求标头中使用HTML链接 [英] How to set the "Content-Type ... charset" in the request header using a HTML link

查看:1857
本文介绍了如何设置“Content-Type ...字符集”在请求标头中使用HTML链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有UTF-8编码链接的简单HTML页面。

I have a simple HTML-page with a UTF-8 encoded link.

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <a charset='UTF-8' href='http://server/search?q=%C3%BC'>search for "ü"</a>
  </body>
</html>


$ b < application / x-www-form-urlencoded; charset = utf-8 到请求头。因此,我必须配置Web服务器,以假设所有请求都是UTF-8编码(URIEncoding =UTF-8在Tomcat server.xml)。但是当然,管理员不会让我在生产环境(Websphere)中这样做。

However, I don't get the browser to include Content-Type:application/x-www-form-urlencoded; charset=utf-8 into the request header. Therefore I have to configure the webserver to assume all requests are UTF-8 encoded (URIEncoding="UTF-8" in Tomcat server.xml). But of course the admin won't let me do that in the production environment (Websphere).

我知道使用Ajax很容易实现,但是如何控制使用标准HTML链接时的请求标头? charset 属性似乎不适用于我(在IE8和FF 3.5测试)

I know it's quite easy to achieve using Ajax, but how can I control the request header when using standard HTML links? The charset attribute doesn't seem to work for me (tested in IE8 and FF 3.5)

第二部分的所需解决方案是在使用Javascript更改IFrame的 document.location 时设置URL编码。

The 2nd part of the required solution would be to set the URL encoding when changing an IFrame's document.location using Javascript.

推荐答案

这不可能从HTML开始。您可以得到的最接近的是< form> accept-charset 属性。只有MSIE浏览器坚持,但即使那样做是错误的(例如,当它说它已经发送ISO-8859-1时,实际上已经使用CP1252)。其他浏览器完全忽略它,并且它们使用响应的 Content-Type 标头中指定的字符集。设置字符编码权利基本上完全是服务器端的职责。客户端应该发送回相同的字符集,因为服务器发送了响应。

This is not possible from HTML on. The closest what you can get is the accept-charset attribute of the <form>. Only MSIE browser adheres that, but even then it is doing it wrong (e.g. CP1252 is actually been used when it says that it has sent ISO-8859-1). Other browsers are fully ignoring it and they are using the charset as specified in the Content-Type header of the response. Setting the character encoding right is basically fully the responsiblity of the server side. The client side should just send it back in the same charset as the server has sent the response in.

到这一点,你应该真正配置字符编码的东西完全从服务器端。为了克服无法编辑 URIEncoding 属性,SO上的某人写了一个(复杂)过滤器:在Tomcat中自动检测URI编码

To the point, you should really configure the character encoding stuff entirely from the server side on. To overcome the inability to edit URIEncoding attribute, someone here on SO wrote a (complex) filter: Detect the URI encoding automatically in Tomcat. You may find it useful as well (note: I haven't tested it).

更新

Update: Noted should be that the meta tag as given in your question is ignored when the content is been transferred over HTTP. Instead, the HTTP response Content-Type header will be used to determine the content type and character encoding. You can determine the HTTP header with for example Firebug, in the Net panel.

这篇关于如何设置“Content-Type ...字符集”在请求标头中使用HTML链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆