通过 AJAX 发布时字符编码错误

发布于 2024-10-09 01:35:45 字数 2207 浏览 2 评论 0原文

我的 Struts2 应用程序存在一些编码问题。我的JSP页面显示2个表单;当我发送第一个(简单的常规形式,重新加载整个页面)时,非标准字符,例如 áñ已正确发送并显示。但是,当我对第二种表单(通过 AJAX 发送)执行相同操作时,数据会被损坏(àíà! 等)。

ma​​in.jsp:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    ...
    <s:head theme="ajax"/>
</head>
<body>
    <!-- This form is sent via regular HTTP request -->
    <s:form theme="simple" enctype="multipart/form-data">
        <s:textfield key="field1" name="var1"/>
        <s:submit key="send" action="SAVE_ACTION"/>
    </s:form>
    <div id="ajaxContainer">
         <jsp:include file="ajax-part.jsp"/>
    </div>
</body>
</html>

ajax-part.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!-- This form is sent via AJAX -->
<s:form enctype="multipart/form-data" theme="ajax" action="AJAX_ACTION" acceptcharset="UTF-8">
    <s:textfield key="field2" name="var2"/>
    <s:submit key="send" targets="ajaxContainer"/>
</s:form>

正如你所看到的,我已经在所有地方设置了 UTF-8 编码。我还检查过这些文件实际上是 UTF-8 编码的。并且 DOJO(用于 AJAX 请求)也以 UTF-8 进行配置,因为 变为:

<script language="JavaScript" type="text/javascript">
    // Dojo configuration
    djConfig = {
        baseRelativePath: "/myApp/struts/dojo",
        isDebug: false,
        bindEncoding: "UTF-8",
        debugAtAllCosts: false
    };
</script>

我在 a 上运行应用程序Tomcat 6服务器;添加 -Dfile.encoding=UTF-8 参数没有任何区别(除了以 UTF-8 而不是 ISO-8859-1 记录的日志文件)。

它在 Google Chrome 和 IE8 中都会发生,所以我猜浏览器与问题无关,它一定是 web 应用程序中的问题。

我缺少什么?我可以更改什么来正确处理我的 AJAX 帖子?

I've a Struts2 application with some encoding problems. My JSP page displays 2 forms; when I send the first one (a simple, regular form, which reloads the full page), non-standard characters such as á or ñ are sent and shown correctly. However, when I do the same with the second form (which is sent via AJAX), the data gets corrupted (Ãí, Ã! and so on).

main.jsp:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    ...
    <s:head theme="ajax"/>
</head>
<body>
    <!-- This form is sent via regular HTTP request -->
    <s:form theme="simple" enctype="multipart/form-data">
        <s:textfield key="field1" name="var1"/>
        <s:submit key="send" action="SAVE_ACTION"/>
    </s:form>
    <div id="ajaxContainer">
         <jsp:include file="ajax-part.jsp"/>
    </div>
</body>
</html>

ajax-part.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!-- This form is sent via AJAX -->
<s:form enctype="multipart/form-data" theme="ajax" action="AJAX_ACTION" acceptcharset="UTF-8">
    <s:textfield key="field2" name="var2"/>
    <s:submit key="send" targets="ajaxContainer"/>
</s:form>

As you can see, I've set UTF-8 encoding everywhere. I've also checked that the files are actually UTF-8 encoded. And DOJO (which is used for the AJAX requests) is configured in UTF-8 too, because <s:head theme="ajax"/> becomes:

<script language="JavaScript" type="text/javascript">
    // Dojo configuration
    djConfig = {
        baseRelativePath: "/myApp/struts/dojo",
        isDebug: false,
        bindEncoding: "UTF-8",
        debugAtAllCosts: false
    };
</script>

I'm running the application on a Tomcat 6 server; adding the -Dfile.encoding=UTF-8 parameter makes no difference (appart from the log files being recorded in UTF-8 instead of ISO-8859-1).

It happens both in Google Chrome and IE8, so I guess the browser has nothing to do with the problem, it must be something in the webapp.

What am I missing? What can I change to process correctly my AJAX posts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

镜花水月 2024-10-16 01:35:45

解决了!

在使用 Firebug 检查 AJAX 请求后(感谢四元数的提示),我发现了问题:Dojo 正确发送了 GET 请求中的数据,参数是 URL 编码的。首先,我认为 Struts2 使用错误的字符集进行 URL 解码,从而损坏了数据;但正如 Steven Benitez 指出的那样,它实际上是底层的 Servlet API。

最终通过在 Tomcat 的 server.xml 上将 URIEncoding="UTF-8" 设置为 解决了这个问题,如 Apache Tomcat 常见问题

如果此方法对其他人不起作用,也可以通过在接收 AJAX 数据的位置添加以下转换来手动解决

new String(myData.getBytes("ISO-8859-1"), "UTF-8");

Solved!

After inspecting AJAX requests with Firebug (thanks for the tip, Quaternion) I found out the problem: Dojo was sending correctly the data in GET requests, with the parameters URL-encoded. First I thought that Struts2 was doing the URL-decoding with the wrong charset, thus corrupting the data; but as Steven Benitez pointed out, it was actually the underlying Servlet API.

The problem was finally solved by setting URIEncoding="UTF-8" to the <Connector> on Tomcat's server.xml, as explained in Apache Tomcat FAQs.

In case this approach doesn't work for somebody else, it could also be manually solved by adding the following conversion where the AJAX data is received:

new String(myData.getBytes("ISO-8859-1"), "UTF-8");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文