1. 程式人生 > >location.href 傳中文引數亂碼問題

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  傳中文的亂碼問題