2、如tomcat这样的服务器,在conf/server.xml里面修改http连接使用utf-8编码,可以解决get传参乱码。
3、同样是tomcat,post传参需要设置字符过滤器可解决。
一:Js的Url中传递中文参数乱码问题,重点:encodeURI编码,decodeURI解码:1.传参页面
Javascript代码:<script type=”text/javascript”>// <![CDATA[
function send(){
var url = "test01.html"
var userName = $("#userName").html()
window.open(encodeURI(url + "?userName=" + userName))}
// ]]>
</script>
2. 接收参数页面:test02.html
<script>
var urlinfo = window.location.href//获取url
var userName = urlinfo.split(“?”)[1].split(“=”)[1]//拆分url得到”=”後面的参数
$(“#userName”).html(decodeURI(userName))
</script>
二:如何获取Url“?”后,“=”的参数值:
A.首先用window.location.href获取到全部url值。
B.用split截取“?”后的全部
C.split(“?”)后面的[1]内数字,默认从0开始计算
三:Js中escape,unescape,encodeURI,encodeURIComponent区别:
1.传递参数时候使用,encodeURIComponent否则url中很容易被”#”,”?”,”&”等敏感符号隔断。
2.url跳转时候使用,编码用encodeURI,解码用decodeURI。
3.escape() 只是为0-255以外 ASCII字符 做转换工作,转换成的 %u**** 这样的码,如果要用更多的字符如 UTF-8字符库 就一定要用 encodeURIComponent() 或 encodeURI() 转换才可以成 %nn%nn 这的码才可以,其它情况下escape,encodeURI,encodeURIComponent编码结果相同,所以为了全球的统一化进程,在用 encodeURIComponent() 或 encodeURI() 代替 escape() 使用
1.客户端提交时使用encodeURI或者encodeurIcompent参数来进行utf-8编码,然后发送给服务器,如果使用post方式的话可以通过request.setCharacterEncoding来设定用来解读参数的编码。如果用get方式的话,使用new String(arg.getBytes("iso-8859-1"),"utf-8")的方式来转码。2.发送ajax请求时,请使用post方式,由于AJAX默认使用utf-8的编码提交参数,因此如果不是文件上传得话,为ajax的Content-Type请求头设置"application/x-www-form-urlencoded;charset="utf-8",以此来通知服务器,客户端发送参数所使用的编码。这样,服务器端可以直接通过String word = request.getParameter("word")来获取经过转码后的参数值,省去了request.setCharacterEncoding。