为什么 POST 不支持字符集,但 AJAX 请求可以?雄猫6 [英] Why does POST not honor charset, but an AJAX request does? tomcat 6

查看:16
本文介绍了为什么 POST 不支持字符集,但 AJAX 请求可以?雄猫6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 tomcat 的应用程序,需要提交一个能够处理 utf-8 字符的表单.当通过ajax提交时,数据从utf-8中的getParameter()正确返回.通过表单提交提交时,数据从 iso-8859-1 中的 getParameter() 返回.

I have a tomcat based application that needs to submit a form capable of handling utf-8 characters. When submitted via ajax, the data is returned correctly from getParameter() in utf-8. When submitting via form post, the data is returned from getParameter() in iso-8859-1.

我使用了 fiddler,并且确定了请求中唯一的不同之处在于 charset=utf-8 被附加到 Content- 的末尾在 ajax 调用中输入 标头(正如预期的那样,因为我明确发送了内容类型).

I used fiddler, and have determined the only difference in the requests, is that charset=utf-8 is appended to the end of the Content-Type header in the ajax call (as expected, since I send the content type explicitly).

来自ajax的ContentType:"application/x-www-form-urlencoded; charset=utf-8"

ContentType from ajax: "application/x-www-form-urlencoded; charset=utf-8"

来自表单的内容类型:应用程序/x-www-form-urlencoded"

ContentType from form: "application/x-www-form-urlencoded"

我有以下设置:

ajax post(正确输出字符):

ajax post (outputs chars correctly):

$.ajax( {
  type : "POST",
  url : "blah",
  async : false,
  contentType: "application/x-www-form-urlencoded; charset=utf-8",
  data  : data,
  success : function(data) { 
  }
 });

form post(以iso格式输出字符)

form post (outputs chars in iso)

 <form id="leadform" enctype="application/x-www-form-urlencoded; charset=utf-8" method="post" accept-charset="utf-8" action="{//app/path}">

xml 声明:

<?xml version="1.0" encoding="utf-8"?>

文档类型:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

元标记:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

jvm 参数:

-Dfile.encoding=UTF-8

我也尝试过使用 request.setCharacterEncoding("UTF-8"); 但似乎 tomcat 只是忽略了它.我没有使用 RequestDumper 阀.

I have also tried using request.setCharacterEncoding("UTF-8"); but it seems as if tomcat simply ignores it. I am not using the RequestDumper valve.

据我所知,POST 数据编码主要取决于表单所在的页面编码.据我所知,我的页面正确编码为 utf-8.

From what I've read, POST data encoding is mostly dependent on the page encoding where the form is. As far as I can tell, my page is correctly encoded in utf-8.

此页面中的示例 JSP 工作正常.它只是使用 setCharacterEncoding("UTF-8"); 并回显您发布的数据.http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

The sample JSP from this page works correctly. It simply uses setCharacterEncoding("UTF-8"); and echos the data you post. http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

总而言之,尽管页面是 utf-8、表单参数指定 utf-8、xml 声明或其他任何内容,但 post 请求不会将字符集发送为 utf-8.我已经花了三天的大部分时间在这上面,但我的想法已经用完了.有人可以帮我吗?

So to summarize, the post request does not send the charset as being utf-8, despite the page being in utf-8, the form parameters specifying utf-8, the xml declaration or anything else. I have spent the better part of three days on this and am running out of ideas. Can anyone help me?

推荐答案

form post(以iso格式输出字符)

form post (outputs chars in iso)

<form id="leadform" enctype="application/x-www-form-urlencoded; charset=utf-8" method="post" accept-charset="utf-8" action="{//app/path}">

您不需要在那里指定字符集.浏览器将使用 HTTP 中指定的字符集响应头.

You don't need to specify the charset there. The browser will use the charset which is specified in HTTP response header.

<form id="leadform" method="post" action="{//app/path}">

足够了.

xml 声明:

<?xml version="1.0" encoding="utf-8"?>

无关紧要.它仅与 XML 解析器相关.Webbrowsers 不会将 text/html 解析为 XML.这仅与服务器端相关(如果您使用基于 XML 的视图技术,如 Facelets 或 JSPX,则在普通 JSP 上这是多余的).

Irrelevant. It's only relevant for XML parsers. Webbrowsers doesn't parse text/html as XML. This is only relevant for the server side (if you're using a XML based view technology like Facelets or JSPX, on plain JSP this is superfluous).

文档类型:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

无关紧要.它仅与 HTML 解析器相关.此外,它没有指定任何字符集.相反,将使用 HTTP 响应标头中的那个.如果您不使用基于 XML 的视图技术(如 Facelets 或 JSPX),这也可以很好<!DOCTYPE html>.

Irrelevant. It's only relevant for HTML parsers. Besides, it doesn't specify any charset. Instead, the one in the HTTP response header will be used. If you aren't using a XML based view technology like Facelets or JSPX, this can be as good <!DOCTYPE html>.

元标记:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

无关紧要.仅当从本地磁盘查看 HTML 页面或在本地解析 HTML 页面时,它才相关.相反,将使用 HTTP 响应标头中的那个.

Irrelevant. It's only relevant when the HTML page is been viewed from local disk or is to be parsed locally. Instead, the one in the HTTP response header will be used.

jvm 参数:

-Dfile.encoding=UTF-8

无关紧要.解析源文件只与 Sun/Oracle(!) JVM 相关.

Irrelevant. It's only relevant to Sun/Oracle(!) JVM to parse the source files.

我也尝试过使用 request.setCharacterEncoding("UTF-8"); 但似乎 tomcat 只是忽略了它.我没有使用 RequestDumper 阀.

I have also tried using request.setCharacterEncoding("UTF-8"); but it seems as if tomcat simply ignores it. I am not using the RequestDumper valve.

这仅在尚未解析请求正文时有效(即您尚未事先调用 getParameter() 等).您需要尽早调用此方法.Filter 是一个完美的地方.否则会被忽略.

This will only work when the request body is not been parsed yet (i.e. you haven't called getParameter() and so on beforehand). You need to call this as early as possible. A Filter is a perfect place for this. Otherwise it will be ignored.

据我所知,POST 数据编码主要取决于表单所在的页面编码.据我所知,我的页面正确编码为 utf-8.

From what I've read, POST data encoding is mostly dependent on the page encoding where the form is. As far as I can tell, my page is correctly encoded in utf-8.

它依赖于 HTTP 响应头.

It's dependent on the HTTP response header.

您需要做的就是以下三件事:

All you need to do are the following three things:

  1. 将以下内容添加到您的 JSP 顶部:

  1. Add the following to top of your JSP:

<%@page pageEncoding="UTF-8" %>

这会将响应编码设置为 UTF-8,并将响应头设置为 UTF-8.

This will set the response encoding to UTF-8 and set the response header to UTF-8.

创建一个 Filter,它在 doFilter() 方法中执行以下操作:

Create a Filter which does the following in doFilter() method:

if (request.getCharacterEncoding() == null) {
    request.setCharacterEncoding("UTF-8");
}
chain.doFilter(request, response);

这将使 POST 请求正文将被处理为 UTF-8.

This will make that the POST request body will be processed as UTF-8.

Tomcat/conf/server.xml 中的 条目更改如下:

Change the <Connector> entry in Tomcat/conf/server.xml as follows:

<Connector (...) URIEncoding="UTF-8" />

这将使 GET 查询字符串将被处理为 UTF-8.

This will make that the GET query strings will be processed as UTF-8.

另见:

  • Unicode - 如何正确获取字符? - 包含适用于 Java EE Web 开发人员的实用背景信息和详细解决方案.
  • See also:

    • Unicode - How to get characters right? - contains practical background information and detailed solutions for Java EE web developers.
    • 这篇关于为什么 POST 不支持字符集,但 AJAX 请求可以?雄猫6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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