1. 程式人生 > >encodeURIComponent 處理URL中特殊字元

encodeURIComponent 處理URL中特殊字元

url引數中特殊字元(如中文,”&”,”/”等)的處理,這些符號需要先經過編碼處理再傳遞到後臺,後臺解碼得到字串


方法1:使用encodeURIComponent()函式引數兩次編碼如下

前臺 js:
window.location.href=ctx+”/proposal/proposalList?flag=init&cc=”+encodeURIComponent(encodeURIComponent(‘張三&’));

後臺 java:
String cc = URLDecoder.decode(Struts2Utils.getParameter(“cc”), “UTF-8”);

方法2:前臺使用encodeURIComponent()函式引數一次編碼

前臺 js:
window.location.href=ctx+”/proposal/proposalList?flag=init&cc=”+encodeURIComponent(‘張三&’);

後臺 java:
String cc = new String(Struts2Utils.getParameter(“cc”).getBytes(“ISO8859-1”), “UTF-8”);