gpt4 book ai didi

java - httpheader 中的编码问题导致 struts 2

转载 作者:行者123 更新时间:2023-12-01 05:02:51 24 4
gpt4 key购买 nike

我正在 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");

然后该消息包含在文件的 header 中,以便可以在 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'

但没有任何成功。

我如何知道错误消息和 header 使用哪种编码,并能够为其选择不同的编码?

谢谢。

最佳答案

非 ASCII 文本的 header 字段编码定义如下: http://www.ietf.org/rfc/rfc2047.txt

这本书并不难读,您会在那里找到答案。编 jetty 字段的示例是

 =?iso-8859-1?q?this=20is=20some=20text?=

关于java - httpheader 中的编码问题导致 struts 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13165724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com