httpheader中的编码问题导致struts 2

时间:2012-10-31 18:55:40

标签: java jquery ajax encoding struts2

我在struts 2应用程序中发送标题中的自定义错误消息。我通过在struts.xml文件中添加一个全局结果来完成它,如:

<global-results>
    <result name="badDataError" type="httpheader">
       <param name="status">500</param>
       <param name="headers.errorMessage">${exception.message}</param>
    </result>
</global-results>

<global-exception-mappings>
    <exception-mapping result="badDataError" exception="mypackage.BadDataException" />
</global-exception-mappings>

所以当我抛出像

这样的异常时
throw new BadDataException("my error message");

然后该消息包含在文件的标题中,因此可以在json中读取它作为错误:

$.ajax(
    { 
        url: ...,
        type: "POST",
        data: ...,
        success: function(data, textStatus) {
            alert("save works");
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            var errorMessage = XMLHttpRequest.getResponseHeader('errorMessage'); 
            var message = "There has been an error";
            if (errorMessage != null){
                message = message + ':<br/>'+errorMessage;
            }
            alert(message);
        },
        dataType: "json"
    }
);    

这有效,但每当我发送带有任何特殊字符的消息(例如áéñ...)时,它都没有在警报中显示或通过console.log()显示变量,但是如果我使用firebug消息显示正确,好像它没有在javascript中使用正确的编码。

我试过设置ajax调用

contenType: 'application/x-www-form-urlencoded; charset=UTF-8'
or
contenType: 'application/x-www-form-urlencoded; charset=ISO-8859-1'

但没有任何成功。

我如何知道它用于错误消息和标题的编码,并能够为其选择不同的编码?

感谢。

1 个答案:

答案 0 :(得分:0)

非ASCII文本的标题字段编码在此处定义:http://www.ietf.org/rfc/rfc2047.txt

这并不难读,你会找到答案。编码头字段的示例是

 =?iso-8859-1?q?this=20is=20some=20text?=
相关问题