特殊字符 js处理

时间:2021-10-13 17:53:08

2.特殊字符传递过程中的处理

(1)js页面的处理

var url= "#@+&这些带有特殊字符";
url=encodeURI(encodeURI(url));//转码两次,很关键
url = url.replace(/\+/g,"+");
url = url.replace(/\&/g,"&");
url = url.replace(/\#/g,"#");

(2)Controller层java类的处理

//获得ajax传来的字符串

String memberName = request.getParameter("memberName");

//将+号编码,否者解码会消失

memberName = StringUtil.replaceAll(memberName, "+", "+");
try {

//对字符串进行解码
   memberName =
java.net.URLDecoder.decode(memberName, "UTF-8");
  } catch
(UnsupportedEncodingException e1) {
   e1.printStackTrace();

}