日期:2014-05-16  浏览次数:20622 次

求教各位大神,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


不知该如何解决此问题?

java代码

@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;
}


js代码


    function doit()
    {
        var xmlhttp;
        var result;
        if (window.XMLHttpRequest)
        {
            xmlhttp = new XMLHttpRequest();
        }
        else
        {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function ()
        {