最新历史版本 :集成后参数值显示乱码 返回文档
编辑时间: 内容长度:图片数:目录数: 修改原因:

目录:

1. 概述编辑

1.1 问题描述

报表已集成到 Web 页面中,通过在页面传递参数至报表中时,会发现有时某些参数值,传递到报表中是显示为问号(???)或乱码等等一系列不能正常显示的情况。

1.2 原因分析

这是由于浏览器和报表服务器的编码不同,字符多次进行编码转换时出现错误导致字符的显示出现乱码,尤其是中日韩文和特殊字符更容易出现乱码问题。

详细的编码原理可参考文档: 编码

1.3 解决思路

在给报表服务器发送请求之前,使用 JavaScript 先对 URL 编码,然后再向服务器提交。避免了不同的操作系统、不同的浏览器、不同的网页字符集,导致完全不同的编码结果。

因为JavaScript 的输出总是一致的,所以就保证了服务器得到的数据是格式统一的。

对 URL 中的中文,包含模板名、参数名字和参数值,进行 encodeURIComponent 或者 encodeURI 编码。

方法  不会对下列字符编码  使用场合   示例
encodeURI  ASCII 字母、数字、~!@#$&*()=:/,;?+' 如果需要编码整个 URL,然后需要使用这个 URL,那么用encodeURI。   encodeURI("http://localhost:8075/webroot/decision/view/report?viewlet=中文.cpt")
encodeURIComponent  ASCII 字母、数字、~!*()' 如果需要编码 URL 中的参数的时候,那么 encodeURIComponent是最好方法。"http://localhost:8075/webroot/decision/view/report?viewlet="+encodeURIComponent("中文.cpt")



222

所以 encodeURIComponent 比 encodeURI 编码的范围更大。 实际例子来说,encodeURIComponent 会把 http:// 编码成 http%3A%2F%2F 而 encodeURI 却不会。

2. 示例编辑

2.1 对 URL 中的中文进行编码

这里对整个 URL 进行编码,因此使用 encodeURL,如下所示:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script Language="JavaScript">             

function frOpen() {   

window.location=encodeURI("http://localhost:8075/webroot/decision/view/report?viewlet=GettingStarted.cpt&地区=华东")

}       

</script>

</head>

<body>

<input type="button" value="字符转换1" onclick="frOpen()">

</body>

</html>

当然也可以使用encodeURIComponent只对参数进行编码:

window.location="http://localhost:8075/webroot/decision/view/report?viewlet="+encodeURIComponent("中文.cpt")


2.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>

2.3 特殊符号处理

如果在需要进行编码的 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>