location.href 传中文参数乱码问题

传中文查询乱码问题 则需要对要传的参数进行二次编码

例如  window.location.href ="reg.html?mid="+mid+""; 

这样子则会乱码

改成

window.location.href ="reg.html?mid="+  encodeURI(encodeURI(mid))+""; 

在接受的jsp页面使用小脚本

<% String name = java.net.URLDecoder.decode(request.getParameter("mid"), "utf-8");%>

在 controller 中进行解码

String name = java.net.URLDecoder.decode(request.getParameter("mid"), "utf-8");\

在页面用JS中解码:

   var url=window.location.href;//获取本页面的链接地址
   var argsIndex = url .split("?mid=");//进行参数拆分
   var arg = argsIndex[1];//获取到第一个参数
   var dataName = decodeURI(arg, "utf-8");//将转码的参数转码

这样子即解决在用 window.location.href  传中文的乱码问题
 

猜你喜欢

转载自blog.csdn.net/qq_34350964/article/details/84313615