为什么POST没有兑现字符集,但AJAX请求呢? Tomcat的6 [英] Why does POST not honor charset, but an AJAX request does? tomcat 6

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

问题描述

我有一个Tomcat的应用程序,需要提交能够处理UTF-8字符的形式。当通过AJAX提交的,是正确的的getParameter()的UTF-8返回的数据。当通过邮寄形式提交,数据是从的getParameter()的ISO-8859-1返回。

我用小提琴手,并已确定了的只有的的要求不同,是的charset = UTF-8 追加到的末内容 - 键入Ajax调用头(如预期,因为我发送内容明确类型)。

ContentType的从阿贾克斯: 应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8

这是形式的ContentType: 应用程序/ x-WWW的形式urlen codeD

我有以下设置:

阿贾克斯后(输出字符正确):

  $。阿贾克斯({
  键入:POST,
  网址:嗒嗒
  异步:假的,
  的contentType:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8,
  数据:数据,
  成功:功能(数据){
  }
 });
 

表后(在ISO输出字符)

 <形式ID =leadformENCTYPE =应用/的X WWW的形式urlen codeD;字符集= UTF-8的方法=后可接受字符集=utf-8行动={//应用程序/路径}>
 

XML声明:

 < XML版本=1.0编码=UTF-8&GT?;
 

文档类型:

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/ XHTML1-transitional.dtd>
 

meta标签:

 < META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8/>
 

JVM参数:

  -Dfile.encoding = UTF-8
 

我也尝试过使用的 request.setCharacterEncoding(UTF-8); ,但它好像tomcat的简单地忽略它。我不使用的RequestDumper阀。

从我读过,POST数据编码主要是依赖于网页的编码,其中的形式。据我所知,我的网页是正确的连接codeD的UTF-8。

这是此页样本J​​SP正常工作。它只是使用的所以setCharacterEncoding(UTF-8); 并回声您发布的数据。 <一href="http://wiki.apache.org/tomcat/FAQ/CharacterEncoding">http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

总结一下,POST请求不发送的字符集作为UTF-8,尽管页面以UTF-8是,表单参数指定的UTF-8,XML声明或其他任何东西。我花了三天这个更好的一部分,并正在运行的想法。谁能帮我?

解决方案
  

表后(在ISO输出字符)

 &LT;形式ID =leadformENCTYPE =应用/的X WWW的形式urlen codeD;字符集= UTF-8的方法=后可接受字符集=utf-8行动={//应用程序/路径}&GT;
 

您不必指定字符集出现。该浏览器将使用它在HTTP指定的字符集 响应头。

只是

 &LT;形式ID =leadform方法=邮报行动={//应用程序/路径}&GT;
 

就足够了。


  

XML声明:

 &LT; XML版本=1.0编码=UTF-8&GT?;
 

无关。这只是相关的XML解析器。化网页浏览器不分析的text / html 为XML。这是只与服务器端(如果你使用基于XML的视图技术,例如Facelets的或JSPX,在普通的JSP这是多余的)。


  

文档类型:

 &LT;!DOCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/ XHTML1-transitional.dtd&GT;
 

无关。这只是适用于HTML解析器。此外,它没有指定任何字符集。取而代之的是,一个在HTTP响应报头将被使用。如果你没有使用基于XML的视图技术,例如Facelets的或JSPX,这可能是好&LT;!DOCTYPE HTML&GT;


  

meta标签:

 &LT; META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8/&GT;
 

无关。这只是有关在HTML页面被浏览了从本地磁盘或在本地解析。取而代之的是,一个在HTTP响应报头将被使用。


  

JVM参数:

  -Dfile.encoding = UTF-8
 

无关。这只是有关太阳/甲骨文(!)JVM来解析源文件。


  

我也尝试过使用 request.setCharacterEncoding(UTF-8); ,但它好像tomcat的简单地忽略它。我不使用的RequestDumper阀。

在请求主体没有被解析然而,这只会工作(即你不叫的getParameter()等事前)。你需要调用这个趁早。 A 过滤器是这样一个完美的地方。否则,将被忽略。


  

从我读过,POST数据编码主要是依赖于网页的编码,其中的形式。据我所知,我的网页是正确的连接codeD的UTF-8。

这是依赖于HTTP响应头。​​

所有你需要做的是以下三件事情:

  1. 下面添加到你的JSP的顶部:

     &LT;%@页面的pageEncoding =UTF-8%&GT;
     

    这将设置响应编码为UTF-8并设置响应头为UTF-8。

  2. 创建一个过滤器由它来完成以下的的doFilter()方法:

     如果(request.getCharacterEncoding()== NULL){
        request.setCharacterEncoding(UTF-8);
    }
    chain.doFilter(请求,响应);
     

    这将使该POST请求主体将被处理为UTF-8。

  3. 更​​改&LT;连接器&GT; 的Tomcat / conf目录/ server.xml中的条目如下:

     &LT;连接器(...)的URIEncoding =UTF-8/&GT;
     

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

参见:

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.

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).

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

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

I have the following settings:

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 (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 declaration:

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

Doctype:

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

meta tag:

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

jvm parameters:

-Dfile.encoding=UTF-8

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.

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.

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

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 (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}">

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

Just

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

is enough.


xml declaration:

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

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:

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

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 tag:

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

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 parameters:

-Dfile.encoding=UTF-8

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


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.

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.


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.

It's dependent on the HTTP response header.

All you need to do are the following three things:

  1. Add the following to top of your JSP:

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

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

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

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

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

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

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

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

See also:

这篇关于为什么POST没有兑现字符集,但AJAX请求呢? Tomcat的6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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