我正在尝试使用控制器发送一个String,就像对AJAX请求的响应一样。但不幸的是,在客户端,所有角色都变得像'?'。所有字符均来自俄语字母。我会感激任何帮助。谢谢!这是控制器代码:
@RequestMapping("/getfield")
@ResponseBody
public Object getField(@RequestParam String fieldName) {
//returns String full of characters from russian alphabet
return pageBuilder.getMultipleChoiceField(fieldName);
}
AJAX处理:
$('#submit').click(function () {
var drop = document.getElementById('Select_chzn');
var spans = drop.getElementsByTagName('span')[0];
var chosen = spans.innerHTML;
$.post('${pageContext.request.contextPath}/getfield',
{
'fieldName': chosen
},
function (result) {
var content = document.getElementById('content');
alert(result);
content.innerHTML = content.innerHTML + result;
});
setTimeout(function() { $(".chzn-select").chosen(); }, 50);
event.preventDefault();
});
});
答案 0 :(得分:0)
好的,事实证明我必须在发送到客户端之前将所有符号编码为UTF8;所以我必须将String编码为UTF8,然后发送到客户端;
@RequestMapping("/getfield")
@ResponseBody
public Object getField(@RequestParam String fieldName) {
byte[] fieldCharactersArray = null;
try {
fieldCharactersArray = pageBuilder.getMultipleChoiceField(fieldName).
getBytes("UTF8");
} catch (Exception e) {
e.printStackTrace();
}
return fieldCharactersArray;
}
干杯!希望这会对某人有所帮助!