javascript 对http的get请求参数编码encodeURIComponent、encodeURI 和Java 解码

JavaScript 代码encode

functionfindNE(){
 var nd = document.getElementById("NE").value;
 nd = encodeURIComponent(encodeURIComponent(nd));
 alert(nd);
 $.fn.zTree.destroy("netree");
 setting.async.url="common/getNeList.action?seachName="+nd;
 
 $.fn.zTree.init($("#netree"), setting);
 
}

encodeURI 和 encodeURIComponent 的写法一致

java代码decode

String serchName = request.getParameter("seachName");
System.out.println("serchName1::::::::::::::::::::::"+serchName);
if(StringUtils.isNotBlank(serchName)){
 try{
     serchName = java.net.URLDecoder.decode(serchName,"UTF-8");
 }catch (UnsupportedEncodingException e) {
     e.printStackTrace();
 }
}
System.out.println("serchName2::::::::::::::::::::::"+serchName);

注意:URLDecoder.decode 方法中需要指名编码格式为 UTF-8,不然会存在编码乱码问题。

 

参考博客:https://www.cnblogs.com/mjbrian/p/6823843.html

 

posted @ 2020-12-23 15:37  渊渟岳  阅读(807)  评论(0编辑  收藏  举报