求教各位大神,java用URLEncoder.encode编码,js用decodeURI解码不完全的问题
求教各位大神,问题是这样的,前台是一段ajax代码,请求后台的一个方法,返回json数组,但是json数组中带有中文,经几番设置编码后无果决定改用编码方式解决,java代码中用的是URLEncoder.encode(targetresultlog.getDetail(),"UTF-8")方法,js中用的是result = decodeURI(result)。这样做编码问题解决了,但是decodeURI解码不完全,解出的字符串是这种:
创建文件:C%3ADocuments+and+SettingsAll+UsersApplication+DataVMware
创建文件:C:Documents and SettingsAll UsersApplication DataVMware
@RequestMapping(value="/viewwindow/ajax/{practicelogid}")
@ResponseBody
public String viewWindowAjax(@PathVariable Long practicelogid,HttpServletResponse response){
//TargetresultlogManager targetresultlogManager = new TargetresultlogManager();
Targetresultlog targetresultlog = targetresultlogManager.getLatestTargetresultlog();
String json = "{";
try {
json += "type:"" + URLEncoder.encode(Constants.SCORENORM_TYPE.get(targetresultlog.getType()),"UTF-8") + "",";
//json += "type:你好"" + Constants.SCORENORM_TYPE.get(targetresultlog.getType()) + "",";
json += "detail:"" + URLEncoder.encode(targetresultlog.getDetail(),"UTF-8") + ""}";
//json += "detail:"" + targetresultlog.getDetail() + ""}";
} catch (Exception e) {
e.printStackTrace();
}
response.setContentType("text/xml;charset=UTF-8");
//response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control", "no-cache");
//model.addAttribute("practicelogid", practicelogid);
return json;
}
function doit()
{
var xmlhttp;
var result;
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function ()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
result = xmlhttp.responseText;
result = decodeURI(result);
var today = new Date();
var json = eval("(" + result + ")");
var bodyStr = json.type + ":" + json.detail + "<br />";
document.getElementById("infoContainer").innerHTML = today + bodyStr + document.getElementById("infoContainer").innerHTML;
//alert(bodyStr);
}
}
xmlhttp.open("POST","${ctx}/train/practicelog/viewwindow/ajax/${practicelogid}",true);
xmlhttp.send();
}
创建文件:C%3ADocuments+and+SettingsAll+UsersApplication+DataVMware