历史版本17 :集成后参数值显示乱码 返回文档
编辑时间:
内容长度:图片数:目录数:
修改原因:
1. 问题描述编辑
报表已集成到Web页面中,通过在页面传递参数至报表中时,会发现有时某些参数值,传递到报表中是显示为问号(???)或乱码等等一系列不能正常显示的情况。
2. 问题原因编辑
这是由于浏览器和报表服务器的编码不同,字符多次进行编码转换时出现错误导致字符的显示出现乱码,尤其是中日韩文和特殊字符更容易出现乱码问题。详细的编码原理,可参考编码文档
3. 解决方案编辑
在给报表服务器发送请求之前,使用Javascript先对URL编码,然后再向服务器提交。避免了不同的操作系统、不同的浏览器、不同的网页字符集,导致完全不同的编码结果。因为Javascript的输出总是一致的,所以就保证了服务器得到的数据是格式统一的。
对URL中的中文,包含模板名、参数名字和参数值,进行encodeURIComponent()编码。
模板中有中文名:window.location="http://localhost:8075/webroot/decision/view/report?viewlet="+encodeURIComponent("中文.cpt")
参数和参数值中有中文:window.location="http://localhost:8075/webroot/decision/view/report?viewlet=GettingStarted.cpt&"+encodeURIComponent("地区")+"="+encodeURIComponent("华东")
4. 示例编辑
4.1 对URL中的中文进行编码
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script Language="JavaScript">
function frOpen() {
window.location="http://localhost:8075/webroot/decision/view/report?viewlet=doc/Advanced/"+encodeURIComponent("列分栏.cpt")
}
</script>
</head>
<body>
<input type="button" value="字符转换1" onclick="frOpen()">
</body>
</html>
4.2 对Form表单中的中文进行编码
如果是以Form表单把参数提交到报表里面,也同样需要在提交前调用encodeURIComponent()进行编码转换,如下例子
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script>
function autoSubmit() {
var Region1 = document.getElementById('Region'); //获取到参数Region所在文本框
Region1.value = encodeURIComponent(Region.value); //对值参数值进行编码转化
Region1.name = encodeURIComponent("地区"); //对参数控件名编码转换,如果参数名字为英文,则不需要此操作
document.FRform.submit();
}
</script>
<body>
<form name=FRform method=post action="http://localhost:8075/webroot/decision/view/report?viewlet=doc/Primary/Parameter/Parameter.cpt">
<input type="text" id="Region" name="地区" value="华东">
<input type="button" name="show" value= "查看" onclick="autoSubmit()"/>
</body>
</html>
4.3 特殊符号处理
如果在需要进行cjkEncode的URI的参数中包含特殊字符,比如%,#,$,=,&,/,?,+,@等字符时,同样使用encodeURIComponent对这些特殊字符进行编码。
例如参数值是”%华%“这样的字符,完整代码如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script Language="JavaScript">
function frOpen() {
window.location="http://localhost:8075/webroot/decision/view/report?viewlet=GettingStarted.cpt&"+encodeURIComponent("地区")+"="+encodeURIComponent("%华%")
}
</script>
</head>
<body>
<input type="button" value="字符转换1" onclick="frOpen()">
</body>
</html>