通过AJAX发布时坏字符编​​码 [英] Bad character encoding when posting via AJAX

查看:122
本文介绍了通过AJAX发布时坏字符编​​码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Struts2的应用程序有一些编码的问题。我的JSP页面显示2种形式;当我发送的第一个(简单,常规的形式,重新加载整个页面)的非标准字符的如 A ñ发送和正确显示。然而,当我做同样的第二种形式(这是通过AJAX发送),数据被破坏( AI A!等)。

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>

正如你所看到的,我到处都设置UTF-8编码。我也检查了文件实际上是UTF-8 EN codeD。和Dojo(它用于Ajax请求)配置为UTF-8也是如此,因为&LT; S:头主题=AJAX/&GT; 变为:

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

我运行一个Tomcat 6服务器上的应用;加入 -Dfile.encoding = UTF-8 参数没有区别(APPART从日志文件中被记录为UTF-8,而不是ISO-8859-1)。

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

这种事无论是在谷歌Chrome和IE8,所以我想在浏览器无关的问题,它必须是一些在Web应用程序。

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.

我在想什么?我能更改为正确地处理我的AJAX的帖子?

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

推荐答案

解决了!

检查AJAX请求与萤火虫后(谢谢你的提示,四元的),我发现了这个问题:道场发送正确的数据GET请求带有参数的URL连接codeD 。首先,我认为,Struts2的在做URL解码与错误的字符集,从而造成数据损坏;但作为的史蒂芬贝尼特斯的指出,这实际上是底层Servlet API的。

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.

这个问题终于被的URIEncoding =UTF-8设置为&LT解决;对;连接器&GT Tomcat的server.xml中,如 Apache Tomcat的常见问题解答解释。

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

在情况下,这种方法并不能为别人工作,也有可能是的手动解决的加入下面的转换在其中接收AJAX数据:

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");

这篇关于通过AJAX发布时坏字符编​​码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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