我在Struts 2的應用程序的自定義錯誤消息發送的標題。我已經加入到struts.xml中做了文件中像一個全球性的結果:編碼問題的HTTPHeader結果在Struts 2
<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(),但如果我使用螢火蟲那裏消息顯示正確,就好像它不使用JavaScript中正確的編碼。
我試圖在Ajax調用
contenType: 'application/x-www-form-urlencoded; charset=UTF-8'
or
contenType: 'application/x-www-form-urlencoded; charset=ISO-8859-1'
但沒有任何成功設置。
如何知道哪個編碼用於錯誤信息和標題,並且能夠爲其選擇不同的編碼?
謝謝。對於非ASCII文本
請勿在ajax中使用任何contenttype。我不知道它可能無法正常工作,但至少在不設置內容類型的情況下嘗試。 –