js 传递路径参数到后台的转码和解码
在开发中遇到前端页面需要将一个附件的路径传递后台实现业务逻辑,但不进行编码一直报404的错误,上代码。
前端编码:JavaScript函数encodeURL()
说明:1 .encodeURL函数主要是来对URI来做转码,它默认是采用的UTF-8的编码.
2. UTF-8编码的格式:一个汉字来三个字节构成,每一个字节会转换成16进制的编码,同时添加上%号.
var row = $('#tabgrid20170725163633448815').datagrid('getSelected'); if(row != null){ var reportid = row.id; var fjpath = row.fjpath; fjpath=encodeURI(fjpath); var cerson_id = row.create_id; var cerson_name = row.create_name; var dept = row.main_dept; var deptname=row.main_dept_name; var url="${ctx}/secondPhase/report_content.pt?reportid="+reportid+"&cerson_id="+cerson_id+"&cerson_name="+cerson_name+"&dept="+dept+"&fjpath="+fjpath+"&deptname="+deptname; url=encodeURI(url); window.location.href=url; }
后端解码如下:
String fjpath=(String)dmp.get("fjpath"); if(fjpath!=null){ fjpath=URLDecoder.decode(fjpath,"utf-8"); }